Type.GetProperties不返回任何内容 [英] Type.GetProperties returning nothing

查看:93
本文介绍了Type.GetProperties不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

public class MyClass
{
    public MyClass(Type optionsClassType)
    {
      //A PropertyInfo[0] is returned here
      var test1 = optionsClassType.GetProperties();
      //Even using typeof
      var test2 = typeof(MyClassOptions).GetProperties();
     //Or BindingFlags
      var test3 = typeof(MyClassOptions)
          .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public);
    }
}

public class MyClassOptions
{
    public int MyProperty { get; set; }
}

我无法获得关于MyClassOptionsPropertyInfo[]

I'm unable to get PropertyInfo[] about MyClassOptions, Type.GetProperties always returns an empty array. First I thought that was a framework bug in Xamarin.iOS, but I tested the same code in another project targeting the same framework and it worked just fine.

有人知道这种情况的可能原因吗?

Anyone knows possible causes for this?

编辑

感谢@Fabian Bigler回答,我明白了. 在我的项目中,即使将Linker设置为中等行为,实例化MyClassOptions仍不足以在运行时保留类定义.仅在实际使用实例(例如设置属性)之后,该类才会保留在我的版本中.

Thanks to @Fabian Bigler answer I got it. In my project, even with Linker set to a moderate behavior, instantiating MyClassOptions was not enough to keep the class definition at runtime. Only after actually using the instance(e.g. setting a property) the class is kept in my build.

似乎链接程序用假人替换了未使用"的东西. 由于我将在该项目中大量使用反射,因此我刚刚禁用了链接器,并且一切都再次正常运行.

Seems that linker replaces "unused" stuff with dummies. Since I'll use reflection a lot in this project I've just disabled the Linker and everything is working again.

推荐答案

这段代码对我来说非常好:

This code works perfectly fine for me:

namespace MyNameSpace
{
    public class MyClass
    {
        public MyClass(Type optionsClassType)
        {
            //A PropertyInfo[0] is returned here
            var test1 = optionsClassType.GetProperties();
            //Even using typeof
            var test2 = typeof(MyClassOptions).GetProperties();
            //Or BindingFlags
            var test3 = typeof(MyClassOptions).GetProperties
(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
        }
    }

    public class MyClassOptions
    {
        public int MyProperty { get; set; }
    }
}

在代码中添加了BindingFlags.Instance.有关更多信息,请参见此帖子.

Added BindingFlags.Instance in code. For further information, have a look at this post.

然后从外部调用它:

var options = new MyClassOptions();
    options.MyProperty = 1234;
    var t = options.GetType();
    var c = new MyNameSpace.MyClass(t);


注意:请注意程序集链接器

如果在启用链接器的情况下进行构建,则可能需要在某处使用该类,因此在编译时不会将其删除.有时,仅实例化代码中的类是不够的,链接器可能会检测到从未使用过该实例,并且无论如何都会将其删除.


NOTE: Be careful with assembly linker

If you're building with linker enabled you may need to use the class somewhere, so it will not be ripped off at compile time. Sometimes, only instantiating the class in your code is not enough, the linker may detect that the instance is never used and will remove it anyway.

这篇关于Type.GetProperties不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆