2

I would like to know how to set property into a nested class with reflection on Xamarin portable class library.

I am working on a project with reflection on a Xamarin portable class library.

I have this code:

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

}

on the other hand I have this function:

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

When I have the string property and I use (for example) GetRuntimeProperties() this function returns about 8 properties so the question is:

How I know if the property it my class?

Other example: How do I know if the property it is my class?

Thank you

EDIT:

Invoice and Customers are examples. The main idea is use Generics now the typeof is evident.

I would like use reflection for all cases.

Thank you

EDIT 2: I add more code in the example.

I hope that is clear myself.

Thank you.

Jnavero
  • 353
  • 6
  • 13
  • All the properties and classes in your example are public and should be visible and accessible externally. so `myInvoice.Person.Name` is accessible. From a reflection perspective you can access the type via the `PropertyType` property of `PropertyInfo` ie `property.Name == "Person" && property.PropertyType == typeof(Customer)` – Nkosi Jul 28 '16 at 22:40
  • Yes, it is, but the problem is when I iterate all properties. I do not Know the type dynamically. In this case Customer is correct but if the parameter is T... – Jnavero Jul 28 '16 at 22:44
  • Yes customer is an example but apparently it is not an example of your desired behavior. please provide a [mcve] so that answers can be provided at accurately match your expectations. – Nkosi Jul 28 '16 at 23:53

2 Answers2

0

All the properties and classes in your example are public and should be visible and accessible externally. so myInvoice.Person.Name is accessible. From a reflection perspective you can access the type via the PropertyType property of PropertyInfo

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

OR

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;
        }
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

you can check the name of the PropertyInfo and compare it wit the property you want to confirm (Person). So replace your comment with:

if (property.Name == "Person")
{
    // This is a Person PropertyInfo.

    Person person = value as Person;
    if (person == null)
    {
       // This is not expected. Log and return
    }

    //Now you can access person.Name
}
Dogu Arslan
  • 3,292
  • 24
  • 43