How do i know if a method wasn't derived from base classes C#

Given this piece of code, how do i know using "i" variable that the method wasn't derived from base classes, but it was declared in the most downcasted class? For example, i don't need no GetType(), ToString() etc and so forth methods to be printed about.

MethodInfo[] methods = Type.GetType(
            "Probabilities_Theory.ProbabilitiesTheory").GetMethods();

foreach (var i in methods)
{
    if (!i.IsVirtual) // another condition needed
        Console.WriteLine(i);
}

I don't need to know if it was overriden or not, because for example GetType() method is not virtual for the reason. But i still don't want GetType() to be printed about.

What's common in GetType() and the other methods that are virtual is that they all weren't declared in the most downcasted class.

I could do it this way:

if (i.DeclaringType == typeof(ProbabilitiesTheory))
    Console.WriteLine(i);

But i want my code to be more automatic, more programmic and stuff. Like one doesn't know what type it is.

Upvotes: 2

Views: 82

Answers (1)

Pragmateek
Pragmateek

Reputation: 13354

Try with BindingFlags.DeclaredOnly:

MethodInfo[] methods = Type.GetType("Probabilities_Theory.ProbabilitiesTheory")
                           .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

Upvotes: 1

Related Questions