Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8895067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:40:30+00:00 2026-06-14T23:40:30+00:00

Is there a more formal/failsafe way to check whether a System.Reflection.MethodInfo refers to a

  • 0

Is there a more formal/failsafe way to check whether a System.Reflection.MethodInfo refers to a class’ implementation of IDisposable.Dispose than the following?

System.Reflection.MethodInfo methodInfo;
methodInfo = ...; //methodInfo obtaining code here
bool isDisposeMethod = methodInfo.Name == "Dispose";

I already know the class implements IDisposable and thus that Dispose exists, but I’m using a PostSharp aspect that should perform special functionality when Dispose is called (compared to any other class method).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:40:32+00:00Added an answer on June 14, 2026 at 11:40 pm

    Having:

    class DisposableObject : IDisposable
    {
        public void Dispose()
        {
            //...
        }
    }
    

    You can do:

    Type t = typeof(DisposableObject);
    
    InterfaceMapping m = t.GetInterfaceMap(typeof(IDisposable));
    MethodInfo mi = t.GetMethod("Dispose");
    
    Console.WriteLine(mi == m.TargetMethods[0]); //true
    

    So, I suppose that you have the MethodInfo for some Dispose method in your class (here mi, simply through GetMethod(string)). Then you’ll need to get an InterfaceMapping Structure object for the IDisposable implementation in the declaring type (here DisposableObject) through Type.GetInterfaceMap Method . There you have TargetMethods referencing the methods really implementing the interface. So, we only need to check whether your reference equals to m.TargetMethods[0] as IDisposable declares only one method.

    From MSDN:

    InterfaceMapping Structure

    Retrieves the mapping of an interface into the actual methods on a
    class that implements that interface.

    Use the InterfaceMapping structure when a type implements interface
    methods that use method names other than those specified by the
    interface, or when a type implements multiple interfaces which have a
    method with the same name.

    To obtain an InterfaceMapping structure, use the Type.GetInterfaceMap
    method.


    One remark: if your class could implement IDisposable explicitly, then m.TargetMethods[0] would reference the explicit implemetation. So, I’m not sure whether there is any way to get it’s MethodInfo except the InterfaceMapping (See Use Reflection to find Methods that implement explicit interfaces). This situation could be error prone. Check it for your specific issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a
Is there a more efficent way of doing this in terms of memory usage
Is there a more Ruby-ish and cleaner way to get a number to operate
Is there a more efficient way to list files from a bucket in Amazon
Is there a more compact and/or conventional way to write this? With ActiveSheet.QueryTables.Add(Connection:=URL;http://carbon.brighterplanet.com/flights.txt, Destination:=Range(A2))
Is there any formal definition for what makes a problem more fundamental than another?
This works, but feels klunky. Is there a more rails-y way to limit a
Other than the fact that PNG is a more common image format, is there
There are more than one solution for finding the-number-of-digits in a given number. For
There are more or less straightforward ways to emit or listen signals of existing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.