C# 可移植类库和具有嵌套属性的反射

标签 c# reflection xamarin portable-class-library printer-control-language

我想知道如何通过 Xamarin 可移植类库的反射将属性设置到嵌套类中。

我正在开展一个对 Xamarin 可移植类库进行反射(reflection)的项目。

我有这个代码:

public class Customer
{
    public string Name { get; set; }
    public string FirstName { get; set; }
    public int Id { get; set; }        
}

public class Invoice
{
    public string Description { get; set; }
    public int Id { get; set; }

    public Customer Person { get; set; }

    //New Edit...
    //Clases...
    // ... More classes

    public OtherClass N-1 {get; set}
    public MoreClases N {get;set;}

}

另一方面,我有这个功能:

    public void TestCode()
    {
        var myCustomer = new Customer()
        {
            FirstName = "qwerty",
            Name = "asdfgh",
            Id = 1
        };

        var myInvoice = new Invoice()
        {
            Description = "chocos",
            Id = 1,
            Person = myCustomer,
        };

        var properties = myInvoice.GetType().GetRuntimeProperties(); 

        foreach (var property in properties)
        {
            var value = property.GetValue(myInvoice);
            if (value != null)
            {
                if (property.PropertyType.IsGenericParameter)
                {
                    //Have Generic parameters
                }
                else
                {
                    //How I Know if my class == person???
                    // EDIT:
                    // If I Use typeof I must check all clases..
                }
            }
        }
    }

当我拥有字符串属性并使用(例如)GetRuntimeProperties() 时,此函数返回大约 8 个属性,所以问题是:

我如何知道该属性是否属于我的类别?

其他例子: 我如何知道该属性是否属于我的类?

谢谢

编辑:

发票和客户就是例子。主要思想是使用泛型,现在 typeof 很明显。

我想在所有情况下都使用反射。

谢谢

编辑 2: 我在示例中添加了更多代码。

我希望我自己清楚这一点。

谢谢。

最佳答案

示例中的所有属性和类都是公共(public)的,并且应该在外部可见和可访问。所以 myInvoice.Person.Name 是可访问的。从反射的角度来看,您可以通过 PropertyInfo

PropertyType 属性访问类型

property.Name == "person"&& property.PropertyType == typeof(Customer)

foreach (var property in properties)
{
    var propertyType = property.PropertyType;
    var value = property.GetValue(myInvoice);
    if (value != null)
    {
        if (propertyType.IsGenericParameter)
        {
            //Have Generic parameters
        }
        else
        {
            //How I Know if my class == person
            if(propertyType.Name == "Person" && propertyType = typeof(Customer)){
                // class == person
            }   
        }
    }
}

foreach (var property in properties)
{
    var propertyType = property.PropertyType;
    var value = property.GetValue(myInvoice);
    if (value != null)
    {                
        if(value is Customer) {
            // class == person
            var person = value as Customer;
            var name = person.Name;
        }
    }
}

关于C# 可移植类库和具有嵌套属性的反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38647598/

相关文章:

c# - 嵌入式 (ASP.NET) Web 服务器

c# - 同步 c#/asp.net 预订应用程序

scala - 在 Scala 中反射(reflect)父类(super class)的值类型参数,无需 TypeTag

c# - CS0117 C# 'Resource' 不包含 'Drawable' 的定义

xamarin - 编译Xamarin Forms新项目时出错

c# - 前台服务永远不会停止xamarin android

c# - 无法加载文件或程序集 "Microsoft.VisualStudio.Services.Common, Version=12.0.21005.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

c# - 将实体转换为 View 模型以与 Web API 一起使用

c# - 修剪所有字符串属性

c# - 使用反射将通用类字段解析器转换为 Dictionary<String, String>