C# 使用反射从覆盖中获取值
我想从继承类的虚拟属性中获取值,请参见下面的代码: 基类:C# 使用反射从覆盖中获取值,c#,reflection,C#,Reflection,我想从继承类的虚拟属性中获取值,请参见下面的代码: 基类: public class TestBaseClass { public virtual int Index { get; } } public class TestInheritedClass : TestBaseClass { public override int Index => 100; // I want to get this value using reflection } static void M
public class TestBaseClass
{
public virtual int Index { get; }
}
public class TestInheritedClass : TestBaseClass
{
public override int Index => 100; // I want to get this value using reflection
}
static void Main(string[] args)
{
var assemblies = Assembly.LoadFile(args[0]);
foreach(var type in assembly.ExportedTypes)
{
if(type.IsSubclassOf(typeof(TestBaseClass))
{
foreach(var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
{
var value = prop.GetValue(typeof(int), null); // I expect to return 100 from override Index value
}
}
}
}
继承类:
public class TestBaseClass
{
public virtual int Index { get; }
}
public class TestInheritedClass : TestBaseClass
{
public override int Index => 100; // I want to get this value using reflection
}
static void Main(string[] args)
{
var assemblies = Assembly.LoadFile(args[0]);
foreach(var type in assembly.ExportedTypes)
{
if(type.IsSubclassOf(typeof(TestBaseClass))
{
foreach(var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
{
var value = prop.GetValue(typeof(int), null); // I expect to return 100 from override Index value
}
}
}
}
我的代码:
public class TestBaseClass
{
public virtual int Index { get; }
}
public class TestInheritedClass : TestBaseClass
{
public override int Index => 100; // I want to get this value using reflection
}
static void Main(string[] args)
{
var assemblies = Assembly.LoadFile(args[0]);
foreach(var type in assembly.ExportedTypes)
{
if(type.IsSubclassOf(typeof(TestBaseClass))
{
foreach(var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
{
var value = prop.GetValue(typeof(int), null); // I expect to return 100 from override Index value
}
}
}
}
是我错过了什么,还是我把这一切都搞错了?我正在尝试从虚拟财产中获取值100
。有没有办法得到这个值
有没有办法得到这个值
是的,有办法。
让我们先看看你在那里做了什么:
prop.GetValue(typeof(int), null)
您正在使用PropertyInfo.GetValue的重载。它期望从中获取值的对象实例作为第一个参数。而是传递typeof(int)
。这不是TestInheritedClass
的实例。
这里我将忽略第二个参数,因为我们不是在讨论索引器。
您可以在文档中了解该参数
相反,您必须首先创建TestInheritedClass
的实例:
var instance = Activator.CreateInstance(typeof(TestInheritedClass));
然后像这样使用它:
if (type.IsSubclassOf(typeof(TestBaseClass))
{
var instance = Activator.CreateInstance(type);
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
{
var value = prop.GetValue(instance);
}
}
你确实错过了什么Index
是一个实例属性:您需要TestInheritedClass
的实例才能访问它。您应该将该实例传递给prop.GetValue
,但您传递的是typeof(int)
,即Type
对象的实例。TestInheritedClass
的所有实例都将返回相同的值并不重要:运行时不知道这一点,这不是语言的工作方式。它不是静态属性,因此您需要一个实例来实现这一点。您可以使用Activator.CreateInstance并在GetValue调用中使用您需要执行的var obj=Activator.CreateInstance(类型)中的实例
在foreach循环外部,然后将obj
传递给prop.GetValue
方法作为第一个参数作为var value=prop.GetValue(obj,null)代码>非常感谢,我成功了,你们帮了我很大的忙。:)