反射获取FieldInfo对象的类型?

发布于 2024-10-19 05:09:21 字数 1020 浏览 1 评论 0原文

大家好, 我需要访问类 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;
            }
        }
    }

HI All,
I need to access the class SomeClass which is declared has a private field in the Wrapper class, using Reflection so far i have been able to get private field members . How do i cast it back to its original type so that i could access it properties and other members.

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;
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

予囚 2024-10-26 05:09:21

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

然后你可以检查 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
}

I dont know if i understand the question correct. You want the type of the private field (backing field)??

Then you could check the FieldType property of the FieldInfo....

like this:

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
}
So要识趣 2024-10-26 05:09:21

使用 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
    }
}

Use PropertyInfo instead:

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
    }
}
多情癖 2024-10-26 05:09:21

我对你想要做什么仍然有点模糊,但你总是可以在任何对象上 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”,这都以相同的方式工作

I'm still a little fuzzy about what your're trying to do, but you can always GetType() on any object and get its actual run time type and query that for properties field of some other type for example

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))

If you want to pass in instances of Someclass and SomeClass that is also fine, just use GetType() on the instances to get the type that you can then use to find properties of that type as illustrated above. this works the same way regardless if you make the method generic and pass in "T" or if its non-generic and you pass in "object"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文