lundi 6 novembre 2017

C# Why are private methods not shown in this reflection example

I am learning C# and was trying a simple Reflection Example. I am trying to get the names of methods from a class. Here's The code:

using System;
using System.Reflection;

namespace Practice
{
    class ReflectionExamples
    {
        private int Sum(int a, int b)
        {
            return a + b;
        }

        public int GetSum(int a, int b)
        {
            int c = Sum(a, b);

            return c;

        }
    }

    class ReflectionDemo
    {
        public static void Execute() // Main calls this
        {
            var a = typeof(ReflectionExamples);

            MethodInfo[] mi = a.GetMethods(); //Using BindingFlags.NonPublic does not show any results
            foreach (MethodInfo m in mi)
            {
                Console.WriteLine(m.Name);
            }
        }
    }
}

The output from this is(Sum is missing):

GetNum
ToString
Equals
GetHashCode
GetType





Aucun commentaire:

Enregistrer un commentaire