C# PropertyInfo.GetValue(null, null) 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/206198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PropertyInfo.GetValue(null, null) returns null
提问by joreg
i have a class with a static public property called "Info". via reflection i want to get this properties value, so i call:
我有一个名为“Info”的静态公共属性的类。通过反射我想获得这个属性值,所以我调用:
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 类型,并且一个 PluginInfo 类型(实现 IPluginInfo)被初始化并在 Info 属性获取访问器中返回,如下所示:
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. am i missing something obvious here?
info 始终为 null,而 PropertyInfo pi 仍然有效。我在这里遗漏了一些明显的东西吗?
采纳答案by Jon Skeet
Could you create a short but complete program that demonstrates the problem?
你能创建一个简短但完整的程序来演示这个问题吗?
Given that you're talking about plugins, my guessis that you've got the problem of having IPluginInfo defined in two different assemblies. See if this articlehelps at all.
鉴于您在谈论插件,我的猜测是您遇到了在两个不同的程序集中定义 IPluginInfo 的问题。看看这篇文章是否有帮助。
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. That way you can break the debugger and look at the return value before it's lost.
验证它的最简单方法是首先调用pi.GetValue
结果并将其存储在object
变量中,然后在另一行中执行强制转换或“as”。这样你就可以破坏调试器并在返回值丢失之前查看它。
回答by Marc Gravell
My first guess would be that you have re-declared the IPluginInfo interface. All .NET types are scoped by their assembly; if you have the same class file in 2 assemblies, you have 2 different interfaces that happen to have the same name.
我的第一个猜测是您重新声明了 IPluginInfo 接口。所有 .NET 类型都由它们的程序集限定;如果您在 2 个程序集中有相同的类文件,则您有 2 个不同的接口,它们碰巧具有相同的名称。
回答by Joel Coehoorn
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.
我现在就到此为止,因为看起来另外两个人在整理第一部分时已经完成了我的其余答案。
回答by devio
In C#, AS returns null if the value does not match the type. If you write:
在 C# 中,如果值与类型不匹配,AS 将返回 null。如果你写:
object info = pi.GetValue(null, null);
Console.WriteLine(info.GetType().ToString());
what type do you receive?
你收到什么类型的?
回答by joreg
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.
我确实已经在一个单独的 .dll 中拥有了 plugininterface,但是已经将这个 .dll 放在 pluginhosts 目录以及所有插件的目录中。