【问题标题】:Reflection get type of FieldInfo object?反射获取 FieldInfo 对象的类型?
【发布时间】:2011-02-23 11:25:46
【问题描述】:

大家好, 我需要访问在 Wrapper 类中声明具有私有字段的类 SomeClass,使用反射到目前为止,我已经能够获取私有字段成员。如何将其转换回其原始类型,以便我可以访问它的属性和其他成员。

internal class Program
    {
        private static void Main(string[] args)
        {
            Wrapper wrap = new Wrapper
            {
                SOmeProperty = new SomeClass
                {
                    Number = 007
                }
            };

            Type type = wrap.GetType();
            FieldInfo[] infos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (var item in infos)
            {
            }
        }
    }

    internal class SomeClass
    {
        public int Number { get; set; }
    }

    internal class Wrapper
    {
        private SomeClass _tempSomeObj;

        public SomeClass SOmeProperty
        {
            get
            {
                return _tempSomeObj;
            }
            set
            {
                _tempSomeObj = value;
            }
        }
    }

【问题讨论】:

    标签: c# reflection


    【解决方案1】:

    我不知道我是否正确理解了这个问题。您想要私有字段(支持字段)的类型?

    然后你可以检查 FieldInfo 的 FieldType 属性......

    像这样:

    internal class Program
    {
        #region Methods
    
        private static void Main(string[] args)
        {
            var wrap = new Wrapper { SOmeProperty = new SomeClass { Number = 007 } };
            Type type = wrap.GetType();
    
            FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var fieldInfo in fieldInfos)
            {
                if (fieldInfo.FieldType == typeof(SomeClass))
                {
                    Console.WriteLine("Yap!");
                }
            }
        }
    
        #endregion
    }
    
    internal class SomeClass
    {
        #region Properties
    
        public int Number { get; set; }
    
        #endregion
    }
    
    internal class Wrapper
    {
        #region Properties
    
        public SomeClass SOmeProperty { get; set; }
    
        #endregion
    }
    

    【讨论】:

      【解决方案2】:

      改用PropertyInfo

      internal class Program
      {
          private static void Main(string[] args)
          {
              Wrapper wrap = new Wrapper
              {
                  SOmeProperty = new SomeClass
                  {
                      Number = 007
                  }
              };
      
              Type type = wrap.GetType();
              PropertyInfo info = type.GetProperty("SOmeProperty", BindingFlags.NonPublic | BindingFlags.Instance);
              SomeClass value = (SomeClass)info.GetValue(wrap, null);
              // use `value` variable here
          }
      }
      

      【讨论】:

      • 感谢您的回复。我真正想要实现的是。 Inned 能够遍历一个类的属性。这里我需要指定的类是 T 类型。所以我真正想要的是,如果我能动态加载这个类吗?示例:假设“SomeClass”作为 T 参数发送,所以我的类型会先发生变化,我发送“SomeClass”,然后我发送“SomeClass2”等等,而不管我需要能够迭代其属性的类类型。我需要在运行时获取此类型,然后遍历其属性?
      【解决方案3】:

      我仍然对您要执行的操作有点模糊,但您始终可以在任何对象上获取 GetType() 并获取其实际运行时类型并查询其他类型的属性字段,例如

      public void ListPropertiesOfType( object targetObject, Type propertyType ) {
        foreach( var foundProperty in targetObject.GetType( ).GetProperties( ).Where( p => p.PropertyType == propertyType ) ) {
          Console.WriteLine( "Name: {0}, Value: {1}", foundProperty.Name, foundProperty.GetValue( targetObject, null ) );
        }
      }
      
      ListPropertiesOfType(new Wrapper(), typeof(SomeClass))
      ListPropertiesOfType(new Wrapper(), typeof(SomeOtherClass))
      

      如果您想传入 Someclass 和 SomeClass 的实例也可以,只需在实例上使用 GetType() 即可获取类型,然后您可以使用该类型查找该类型的属性,如上图所示。无论您将方法设为泛型并传入“T”,还是将其非泛型并传入“object”

      ,这都以相同的方式工作

      【讨论】:

        猜你喜欢
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-30
        • 1970-01-01
        相关资源
        最近更新 更多