locked
How is System.Type cast to System.Reflection.MethodInfo? RRS feed

  • Question

  • In the Attibutes Sample on MSDN, there is a method whose header is:

     
       private static void DumpAttributes(MemberInfo member)

    There is a call to this method which passes the return value of typeof, i.e. System.Type:

            // display attributes for Order class
            DumpAttributes(typeof(Order));
    

    Can anyone please tell me how it is possible to pass a System.Type into an argument of type System.Reflection.MethodInfo?

    Thank you.

    Friday, February 1, 2013 6:59 PM

Answers

  • System.Type inherits from MemberInfo.

    From the docs for System.Type:

    [SerializableAttribute]
    [ClassInterfaceAttribute(ClassInterfaceType.None)]
    [ComVisibleAttribute(true)]
    public abstract class Type : MemberInfo, _Type, IReflect

    Friday, February 1, 2013 7:57 PM

All replies

  • If you want to Dump Attributes then you should make minor modification.

    private static void DumpAttributes(MemberInfo[] members)

    Next you have to call this method in this way.

    DumpAttributes(typeof(Order).GetMembers());

    or you can use your existing method, and call in this way.

    MemberInfo[] memberInfos = typeof(Form).GetMember("Member Name");
    if (memberInfos.Length>0)
        DumpAttributes(memberInfos[0]);


    Avik Das

    Friday, February 1, 2013 7:18 PM
  • System.Type inherits from MemberInfo.

    From the docs for System.Type:

    [SerializableAttribute]
    [ClassInterfaceAttribute(ClassInterfaceType.None)]
    [ComVisibleAttribute(true)]
    public abstract class Type : MemberInfo, _Type, IReflect

    Friday, February 1, 2013 7:57 PM
  • You can pass System.Type directly. It will work.

    Avik Das

    Friday, February 1, 2013 8:09 PM
  • Thank you for your replies.

    I must have formed my own pre-conceived idea about what System.Type was and didn't expect that it could inherit from MemberInfo so didn't look for that.

    As you may have guessed, I'm new to reflection.

    • Edited by Lost Johnny Thursday, February 7, 2013 11:48 PM
    Thursday, February 7, 2013 11:46 PM