Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 嵌套类和动态调用问题_C#_Reflection_Nested Class - Fatal编程技术网

C# 嵌套类和动态调用问题

C# 嵌套类和动态调用问题,c#,reflection,nested-class,C#,Reflection,Nested Class,我有以下情况: public class Restriction { public string RestrictionName { get; set; } public bool Eval(decimal Value2) { Type typeRestriction = Type.GetType(RestrictionName); return (bool)typeRestriction.InvokeMember("Eval",

我有以下情况:

public class Restriction
{
    public string RestrictionName { get; set; }
    public bool Eval(decimal Value2)
    {
        Type typeRestriction = Type.GetType(RestrictionName);
        return (bool)typeRestriction.InvokeMember("Eval",
            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
            null,
            null,
            new object[] { this, Value2 });
    }


    /* Nested Classes */
    class A
    {
        public static bool Eval(Restriccion r, decimal value)
        {
            // Do something
        }
    }

    class B
    {
        public static bool Eval(Restriccion r, decimal value)
        {
            // Do something
        }
    }
}
当我尝试时:

    Restriction r = new Restriction();
    r.RestrictionName = "A";
    r.Value = 15;
    r.Eval(16);
我得到
System.NullReferenceException:对象引用未设置为对象的实例。

调试显示
typeRestriction
为空。为什么?嵌套类是否有特殊处理?

这是一个名称空间问题,请尝试使用以下内容引用类类型:

Type typeRestriction = Type.GetType(string.Format("Restriction+{0}", RestrictionName));

类型
A
B
在此场景中是嵌套类型。因此,它们的名称分别为
Restriction+A
Restriction+B
。此外,名称中必须包含包含
限制的命名空间。例如,如果名称空间是
ConsoleApplication
,则需要使用名称
ConsoleApplication.Restriction+A
作为
A
的名称

我尝试了
Restriction+{0}
Restriction.{0}
但它不起作用,它只错过了名称空间。如果要从另一个程序集使用此名称,则字符串中还必须包含带有版本号和内容的完全限定程序集名称。但幸运的是,这里的情况并非如此,这是事实。