什么是类型

本文关键字:类型 什么 | 更新日期: 2023-09-27 17:56:37

我在代码中定义Type时遇到问题。让我们从ABC开始。

public partial class Abc {
    public static String AbcName="wtf";
    public String Name {
        get;
        set;
    }
}

好了, Abc完成了。这是之后的测试课程。并且我需要返回带有MyClass实例的对象或类型,这里是

    public partial class MyClass {
        public const BindingFlags
            Universal=BindingFlags.NonPublic|BindingFlags.Public,
            WithObject=Universal|BindingFlags.Instance,
            WithClass=WithObject|BindingFlags.Static,
            ForGive=Universal|BindingFlags.SetProperty|BindingFlags.SetField,
            ForGet=Universal|BindingFlags.GetProperty|BindingFlags.GetField,
            ForDo=BindingFlags.InvokeMethod|WithObject|WithClass;
        public MyClass GetTypeImpl() {
            if(null!=target)
#if TARGET_AS_TYPE
                return new MyClass(target as Type??target.GetType());
#else
                return new MyClass(target.GetType());
#endif
            else
                return new MyClass(typeof(object));
        }
        public object GetValue(String name) {
            var invokeAttr=ForGet|WithClass;
            var type=(Type)this.GetTypeImpl().target;
            return type.InvokeMember(name, invokeAttr, default(Binder), target, default(object[]));
        }
        public MyClass(object x) {
            this.target=x;
        }
        public object target;
    }

请注意,该代码用于表示我的类。 实际代码中的GetValue是在内部调用的,使用代码永远不会获得除MyClass类型以外的对象。也就是说,在实际代码中,MyClass中的每个方法实际上都返回了一个MyClass实例。在这里我们看到条件编译,TARGET_AS_TYPE ,这就是这个问题的重点。

请考虑以下测试代码

    public partial class TestClass {
        public static void TestMethod() {
            var abc=
                new Abc {
                    Name="xyz"
                };
            var x=new MyClass(abc);
            var abcName=x.GetValue("Name");
            var y=new MyClass(x.GetTypeImpl().target);
#if TARGET_AS_TYPE
            var wtf=y.GetValue("AbcName");
            var fullName=y.GetValue("FullName"); // exception thrown
#else
            var fullName=y.GetValue("FullName"); 
            var wtf=y.GetValue("AbcName"); // exception thrown
#endif
        }
    }

无论我们是否定义TARGET_AS_TYPE测试总是在第二行带有 #if#elseif 块时抛出异常。我认为这是因为TypeRuntimeType,但我无法定义它。那么,如何纠正它(在 GetTypeImpl 中)并让它在没有条件编译的情况下始终工作

以下方式受到限制,或者我做了没有效果。

  • 使用BindingFlags.FlattenHierarchy - 不工作
  • 通用MyClass<T>声明 - 不起作用
  • 不要用MyClass实例扭曲类型或对象 - 你一定是在开玩笑......

什么是类型

请考虑以下代码:

Abc a = new Abc { Name = "a" };
Type t = a.GetType();
BindingFlags staticField = BindingFlags.Static | 
                           BindingFlags.Public | 
                           BindingFlags.GetField;
BindingFlags instanceProperty = BindingFlags.Instance | 
                                BindingFlags.Public | 
                                BindingFlags.GetProperty;
//prints a
t.InvokeMember("Name", instanceProperty, default(Binder), a, null);
//prints wtf
t.InvokeMember("AbcName", staticField, default(Binder), a, null);
//throws an exception as there is no member FullName in MyClass
t.InvokeMember("FullName", instanceProperty, default(Binder), a, null);
Type tt = t.GetType();
//prints t.FullName, that is YourNamespace.Abc
tt.InvokeMember("FullName", instanceProperty, default(Binder), t, null);

它显示了问题,你IMO有。不能通过描述类MyClassType 类型的对象访问类 Type 的成员,因为MyClass没有这些成员。

您需要使用描述类TypeType对象(即 a.GetType().GetType() ) 通过反射访问其成员(此处FullName),并将描述类MyClass的类型为 Type 的对象传递给InvokeMember


根据评论,我想在类型为 System.Object 的对象上发布相同的示例:

object o = new object();
Type ot = o.GetType();
BindingFlags instanceMethod = BindingFlags.Instance | 
                                BindingFlags.Public | 
                                BindingFlags.InvokeMethod;
//prints System.Object
ot.InvokeMember("ToString", instanceMethod, default(Binder), o, null);
//throws an exception, 
//as there is obviously also no FullName in class System.Object
ot.InvokeMember("FullName", instanceProperty, default(Binder), o, null);