-1

So this is the code:

class Program
{
    static void Main(string[] args)
    {
        Type T = Type.GetType("CSharpLearningPurposes.Program");

        PropertyInfo[] properties = T.GetProperties();

        foreach (PropertyInfo property in properties)
        {
            Console.WriteLine(property.PropertyType.Name);
        }

    }
}

More specifically, the question is about this line of the code: Console.WriteLine(propert.PropertyType.Name);

You see here that I access property.PropertyType okay I understand that I am accessing the object's member but I don't understand this: property.PropertyType.Name

What's that doing exactly? Can anybody explain me?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
BoSsYyY
  • 563
  • 5
  • 13
  • 1
    It is called [reflection](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection). Googles first hit: [the docs](https://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.propertytype(v=vs.110).aspx) – Peter Bons Feb 23 '18 at 18:36
  • 1
    `PropertyType` is a `Type` object - it's basically metadata about the type (which as you can imagine is useful for inspecting objects at runtime - "reflection") - information on its name etc etc. Here you are writing the name of the type to the console. The type object in question is the type of whichever property you are enumerating during `GetProperties`. For example - it could be a property which gets/sets a `string` value. The `Type` object would be a descriptor for `System.String` and would describe properties, methods, the declaring assembly etc etc from `System.String`. – Charleh Feb 23 '18 at 18:38
  • `PropertyType.Name` will return exactly what the name suggests. The property type name, lol. – Tiramonium Feb 23 '18 at 18:39
  • See here for what you are getting hold of: https://msdn.microsoft.com/en-us/library/system.type – Charleh Feb 23 '18 at 18:43
  • 1
    No no no. My question is completely different. It's not about the reflection. It's about this line of code: property.PropertyType.Name What's happening exactly here? How I'm accessing three member I cannot understand what's going on here. – BoSsYyY Feb 23 '18 at 18:44
  • Is your question just about the statement `X.Y.Z`, where the properties are chained? – BJ Myers Feb 23 '18 at 18:46
  • @Igor It's definitely about how the nested `.` notation works, the fact that reflection is present in the question is irrelevant. – Jonathon Chase Feb 23 '18 at 18:47
  • I reopened your question as I can't find a suitable dupe for it. I do recommend you do some reading though, this is basic c# syntax (*and probably the same syntax for the majority of oop languages*). – Igor Feb 23 '18 at 18:52
  • @BoSsYyY That line is saying get the value of the `Name` property of the object returned by getting the `PropertyType` property of the `property` variable. You're example, and variable names does make this a bit more complex than it needs to be. – juharr Feb 23 '18 at 18:54
  • @Jonathon Chase Aha. I think I got it. The B property allows me to access members of the B class through it because it's of type B. I think i got the idea. Thanks!!! – BoSsYyY Feb 23 '18 at 18:54

1 Answers1

0

Suppose the following classes:

public class A {
    public B Prop {get;set;}
}

public class B {
    public string Name {get;set;}
}

If you had an instance of A setup like so:

var example = new A { Prop = new B { Name = "The Name" } };

Then example.Prop.Name would return "The Name" by way of having returned the instance of B assigned to Prop.

You could break this down into a longer form to get an example of how the access works and see how chaining the calls is helpful from a productivity standpoint.

B propVal = example.Prop;
string nameVal = propVal.Name;
Console.WriteLine(nameVal == example.Prop.Name); // True 
Jonathon Chase
  • 9,396
  • 21
  • 39