MethodInfo w System.Reflection

0

Napisałem prostą klasę i użyłem na nie "reflection":


```using System;
using System.Reflection;
namespace IntroductionIntoReflection
{
    class Program
    {
        static void Main(string[] args)
        {

            Type T = Type.GetType("IntroductionIntoReflection.Car");

            Console.WriteLine("Klasa:");
            Console.WriteLine("Type Name: " + T.Name);
            Console.WriteLine("Type Namespace: " + T.Namespace);
            Console.WriteLine("Type full name: " + T.FullName);
            Console.WriteLine();

            Console.WriteLine("Pola:");
            FieldInfo[] fields = T.GetFields();
            Console.WriteLine(fields.Length);
            foreach (FieldInfo field in fields)
            {
                Console.WriteLine(field.FieldType.Name + " " + field.Name);
            }
            Console.WriteLine();

            Console.WriteLine("Właściwości:");
            PropertyInfo[] properties = T.GetProperties();
            foreach(PropertyInfo property in properties)
            {
                Console.WriteLine(property.PropertyType.Name + " " + property.Name);
            }
            Console.WriteLine();

            Console.WriteLine("Metody:");
            MethodInfo[] methods = T.GetMethods();
            Console.WriteLine(methods.Length);
            foreach (MethodInfo method in methods)
            {
                Console.Write(method.ReturnType + " " + method.Name);
                foreach (Attribute attribute in method.GetCustomAttributes(true))
                {
                    Console.Write(" " + attribute.ToString());
                }
                Console.WriteLine();
            }
            Console.WriteLine();

            Console.WriteLine("Konstruktory:");
            ConstructorInfo[] constructors = T.GetConstructors();
            foreach(ConstructorInfo constructor in constructors)
            {
                Console.WriteLine(constructor.ToString());
            }


            Console.ReadKey();
        }
    }

    class Car 
    {
        private int ID;
        private string Name;
        private static int pop = 0;
        public string Manufacturer { get; set; }
        public Car()
        {
            this.ID = -1;
            this.Name = string.Empty;
            pop += 1;
        }
        public Car(int id, string name)
        {
            this.ID = id;
            this.Name = name;
            pop += 1;
        }
        public int PrintID()
        {
            return this.ID;
        }
        public string PrintName()
        {
            return this.Name;
        }
        public static int PrintPopulation()
        {
            return pop;
        }
    }
}

W załączonym pliku widać, że, poza dwoma właściwościami pola "Manufacturer", znajduje mi trzy metody które zdefiniowałem i jakieś cztery inne. Wie ktoś może co to za cztery ostatnie metody i skąd się tam wzięły?
Pytanie też, co z członkami prywatnymi klasy? rozumiem, że z poziomu odbicia nie mam do nich dostępu. Czy istnieje inny sposób by się do nich dostać? Nie przeszkadza to w "late bindingu"?

4

Chwalebnie, że zainteresowałeś się refleksją.

Mniej chwalebne, ze nie rozpoznajesz najbardziej fundamentalnych metod każdego Objectu. To znaczy, że refleksją być może zaintereowałes się za wcześnie.

Gdybyś nie chciał otrzymać methodInfo dla m.odziedziczonych po przodkach, powinieneś użyć
*The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.DeclaredOnly to search only the methods declared on the Type, not methods that were simply inherited.
*
https://docs.microsoft.com/en-us/dotnet/api/system.type.getmethods?view=netcore-3.1

Pytanie też, co z członkami prywatnymi klasy? rozumiem, że z poziomu odbicia nie mam do nich dostępu. Czy istnieje inny sposób by się do nich dostać? Nie przeszkadza to w "late bindingu"?

j/w dobierając parametry GetMethods.
Myśli o late bindingu nie zrozumiałem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1