gpt4 book ai didi

c# - 反射:Property-GetValue by Class-Type(因此没有实例化的对象)

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:44 24 4
gpt4 key购买 nike

在我们的项目中,我们有多个所谓的Projections。这些 Projection 类都有一个带有 get 的字符串属性,称为 ProjectionTypeName。此 ProjectionTypeName 对于所有投影应该是唯一的。

今天我遇到了两个投影具有相同 ProjectionTypeName 的问题,这造成了很多麻烦。所以我决定进行单元测试以防止这种情况再次发生。

我使用以下方法获取所有 Projections 的所有 Types:

// The excluded abstract projections
private static readonly List<Type> AbstractProjections = new List<Type>
{
typeof(AbstractRavenDbChangesProjection),
typeof(AbstractRavenDbTimelineProjection)
};

private static IEnumerable<Type> GetAllProjectionTypes()
{
return typeof(AbstractRavenDbChangesProjection)
.Assembly
.GetTypes()
.Where(x => typeof(AbstractRavenDbChangesProjection).IsAssignableFrom(x) && !x.IsInterface)
.Except(AbstractProjections)
.ToList();
}

然后我进行了实际测试:

[Test]
public void TestNoDuplicated()
{
var noDuplicatedList = new Dictionary<Type, string>();
var projectionTypes = GetAllProjectionTypes();
foreach (var type in projectionTypes)
{
// TODO: Get projectionTypeName-value by Projection's Type
var projectionTypeName = ??;

Assert.IsFalse(noDuplicatedList.ContainsValue(projectionTypeName),
"{0} has been found as ProjectionTypeName in two different Projections: {1} & {2}",
projectionTypeName,
noDuplicatedList.First(x => x.Value == projectionTypeName).Key,
type);
noDuplicatedList.Add(type, projectionTypeName);
}
}

我四处看了看,甚至尝试了 a piece of code by @JohnSkeet ,即使他说(我引用):

Please don't do this. Ever. It's ghastly. It should be trampled on, cut up into little bits, set on fire, then cut up again. Fun though, isn't it? ;)

但自 2012 年(发布答案时)以来,这似乎已经发生了变化,现在当您尝试这种反射时,.NET 会给出错误:"System.Security.VerificationException : Operation could destabilize the runtime ”

所以,我的问题。当我只有类的 Type 可供我使用(而不是实际实例化的对象)时,如何获取字符串属性 ProjectionTypeName 的值。

如果我有一个实例化的对象,我将能够做这样的事情:

myInstantiatedObject.GetType().GetProperty("ProjectionTypeName")
.GetValue(myInstantiatedObject, null);

最佳答案

改用自定义属性怎么样?

[AttributeUsage(System.AttributeTargets.Class)]
public sealed class ProjectionTypeNameAttribute : Attribute
{
private string m_Name;

public string Name
{
get { return m_Name; }
}

public ProjectionTypeNameAttribute(string name)
{
m_Name = name;
}
}

[ProjectionTypeNameAttribute(ProjectionWhatever.PROJECTION_NAME)]
public class ProjectionWhatever
{
public const string PROJECTION_NAME = "Whatever";

// if you want to have property as well
public string ProjectionTypeName
{
get
{
return PROJECTION_NAME;
}
}
}

// query if type has attribute
var type = typeof(ProjectionWhatever);
var attributes = type.GetCustomAttributes(typeof(ProjectionTypeNameAttribute), false);
if (attributes != null && attributes.Length > 0)
{
var attribute = (ProjectionTypeNameAttribute)attributes[0];
// use attribute.Name
}

关于c# - 反射:Property-GetValue by Class-Type(因此没有实例化的对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32349904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com