简体   繁体   English

PropertyInfo.GetValue(null,null)返回null

[英]PropertyInfo.GetValue(null, null) returns null

i have a class with a static public property called "Info". 我有一个带有静态公共属性的类,称为“ Info”。 via reflection i want to get this properties value, so i call: 通过反射,我想获得此属性值,所以我打电话:

PropertyInfo pi myType.GetProperty("Info");
string info = (string) pi.GetValue(null, null);

this works fine as long as the property is of type string. 只要属性是字符串类型,此方法就可以正常工作。 but actually my property is of type IPluginInfo and a PluginInfo type (implementing IPluginInfo) is instatiated and returned in the Info properties get accessor, like this: 但实际上我的属性的类型为IPluginInfo,并且在Info属性的get访问器中实例化了PluginInfo类型(实现IPluginInfo),如下所示:

public static IPluginInfo PluginInfo
{
    get 
    {
        IPluginInfo Info = new PluginInfo();
        Info.Name = "PluginName";
        Info.Version = "PluginVersion";
        return Info;
    }
}

like this when i call: 当我打电话时是这样的:

IPluginInfo info = pi.GetValue(null, null) as IPluginInfo;

info is always null, whiel PropertyInfo pi is still valid. info始终为null,而PropertyInfo pi仍然有效。 am i missing something obvious here? 我在这里缺少明显的东西吗?

Could you create a short but complete program that demonstrates the problem? 您能否创建一个简短但完整的程序来演示该问题?

Given that you're talking about plugins, my guess is that you've got the problem of having IPluginInfo defined in two different assemblies. 鉴于您在谈论插件,我的猜测是您遇到了在两个不同的程序集中定义IPluginInfo的问题。 See if this article helps at all. 看看本文是否有帮助。

The easiest way to verify it is to call pi.GetValue and store the result in an object variable first, then do the cast or "as" in another line. 验证它的最简单方法是先调用pi.GetValue并将结果存储在object变量中,然后在另一行中进行pi.GetValue或“ as”。 That way you can break the debugger and look at the return value before it's lost. 这样,您可以中断调试器并在丢失之前查看返回值。

My first guess would be that you have re-declared the IPluginInfo interface. 我的第一个猜测是您已经重新声明了IPluginInfo接口。 All .NET types are scoped by their assembly; 所有.NET类型均受其汇编作用域的限制。 if you have the same class file in 2 assemblies, you have 2 different interfaces that happen to have the same name. 如果在2个程序集中有相同的类文件,则有2个不同的接口碰巧具有相同的名称。

ok, thanks for all the answers. 好的,谢谢你的回答。

i indeed already had the plugininterface in a separate .dll but had placed this .dll in the pluginhosts directory as well as in the directory with all the plugins. 我确实已经将plugininterface放在一个单独的.dll中,但是将该.dll放在了pluginhosts目录以及所有插件的目录中。

Um, first of all I'd implement that property a little differently: 嗯,首先我将实现该属性有所不同:

private static PluginInfo _PluginInfo = null;
public static IPluginInfo PluginInfo
{
    get
    {
        if (_PluginInfo == null)
        {
           _PluginInfo = new PluginInfo();
           _PluginInfo.Name = "PluginName";
           _PluginInfo.Version = "PluginVersion";
        }
        return _PluginInfo;
    }
}

Note that this needs a little more work because it isn't threadsafe, but hopefully you get the idea: build it one time rather than repeatedly. 注意,这不是线程安全的,因此需要做更多的工作,但是希望您能想到:一次构建它,而不是重复构建它。

I'll stop here now, since it looks like two others already finished the rest of my answer while putting together the first part. 我现在将在此处停止,因为看起来其他两个人在整理第一部分时已经完成了我的其余回答。

In C#, AS returns null if the value does not match the type. 在C#中,如果值与类型不匹配,则AS返回null。 If you write: 如果您写:

object info = pi.GetValue(null, null);
Console.WriteLine(info.GetType().ToString());

what type do you receive? 你会收到什么类型的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM