jeudi 28 juillet 2016

C# Portable class library and reflection with nested properties

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

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

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

How do I know if the property is my class?

Other example: How I get person.Name value from the Invoice?

Thank you





Aucun commentaire:

Enregistrer un commentaire