0

I'd like to list all method names from the underlyingtype.

I tried

var methods = this.GetType().UnderlyingSystemType.GetMethods();

but doesn't work.

EDIT

Added example

public class BaseClass
{
   public BaseClass()
   {
         var methods = this.GetType().UnderlyingSystemType.GetMethods();
   }
}

public class Class1:BaseClass
{
   public void Method1()
   {}

   public void Method2()
   {}
}

I need in the collection Method1 and Method 2.

4
  • 1
    What is the underlying type? and what gets returned? Need more info please Commented Jan 3, 2012 at 14:14
  • 3
    The underlying type of what? And what happens? "doesn't work" is never a good description. Please read tinyurl.com/so-hints and update your question with more details. Commented Jan 3, 2012 at 14:14
  • Don't you need BaseType instead of UnderlyingSystemType? Commented Jan 3, 2012 at 14:28
  • Doesn't make much more sense to me, but if what you actually wants is Method1 and Method2, see my updated answer... Commented Jan 3, 2012 at 14:40

4 Answers 4

1

The code you provided works.

System.Exception test = new Exception();
var methods = test.GetType().UnderlyingSystemType.GetMethods();

foreach (var t in methods)
{
    Console.WriteLine(t.Name);
}

returns

get_Message
get_Data
GetBaseException
get_InnerException
get_TargetSite
get_StackTrace
get_HelpLink
set_HelpLink
get_Source
set_Source
ToString
GetObjectData
GetType
Equals
GetHashCode
GetType

EDIT:

Is that what you want?

Class1 class1 = new Class1();
var methodsClass1 = class1.GetType().GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

BaseClass baseClass = new BaseClass();
var methodsBaseClass = baseClass.GetType().GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

foreach (var t in methodsClass1.Where(z => methodsBaseClass.FirstOrDefault(y => y.Name == z.Name) == null))
{
    Console.WriteLine(t.Name);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like

MethodInfo[] methodInfos =
typeof(MyClass).GetMethods(BindingFlags.Public |
                                                      BindingFlags.Static);

1 Comment

I need to make this call in the baseclass contructor
0
here is an example on how to use reflection to get the Method names
replace MyObject with your Object / Class

using System.Reflection;
MyObject myObject;//The name of the Object
foreach(MethodInfo method in myObject.GetType().GetMethods())
 {
    Console.WriteLine(method.ToString());
 }

Comments

0

The problem lies in the override of GetType which you are calling in the constructor of the BaseClass.

If you create an instance of the Class1 type, and look at the methods you have, you will see all 6 methods.

If you create an instance of the BaseClass type, you will only see 4 methods - the 4 methods on from the Object type.

By creating an instance of the subclass, you are implicitly calling the constructor in the BaseClass. When it uses GetType(), it uses the overridden virtual method of the Class1 type, which returns the expected response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.