.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Web Links

Reflection question - how to tell if a method is defined in an interface?

Posted By:      Posted Date: October 10, 2010    Points: 0   Category :.NET Framework

OK so the Reflection namespace has tools so you can tell if a method has a base definition in a class (MethodInfo.GetBaseDefinition().DeclaringType). This way for instance I can query the ToString method of my favorite class and see that yes, indeed this inherits from System.Object.

I also can detect if a method is an explicit interface implementation, by checking the name of the method (methods representing explicit interface implementation will have the fully qualified name of the interface pre-pended to the value of the Name property) so I'm okay there.

But what about plain ole interface implementations? How do I get this info?

Thanks if you can help!

View Complete Post

More Related Resource Links

Reflection question - how to tell which exceptions a method throws


I've found an ExceptionHandlingClauses collection in MethodBody but I'm looking for the exceptions that are *thrown*. You know, when you look at the ReadByte method of FileStream on msdn and you see that the ReadByte method throws two exceptions - the NotSupportedException and the ObjectDisposedException. 

How can I get this info programatically?


database interface design question


For handy database interface, is it good to use SqldataSources exclusively intead of EntLib functions?

Thanks for any feedback.

Question in Get method in C#


I got a recent working code from client.I see errors in the get methods when i compile , below are some place where the error happens.Can any one help me out?


Error 2 'System.Environment.NewLine.get': cannot explicitly call operator or accessor C:\Documents and Settings\shankar\My Documents\Visual Studio 2008\Projects\DKCLwin\DKCLwin\LocalizationAdmin.cs 150 170 DKCLwin

Error 2 'System.Environment.NewLine.get': cannot explicitly call operator or accessor C:\Documents and Settings\shankar\My Documents\Visual Studio 2008\Projects\DKCLwin\DKCLwin\LocalizationAdmin.cs 150 170

DataMember and Interface question


I've created a class model that is a data contract:

[DataContract(Name = "OfficeEmployeeDtrModel")]
    public class OfficeEmployeeDtrModel




I want this class to implement an interface:

public class OfficeEmployeeDtrModel: IMyInterface


Problem is, when I add the service reference, I cannot find the interface in the generated service proxy. I was actually doing this so I can use the interface as function arguments in my client as to avoid multiple function blocks. Can anyone advise me as to what to do?

Dependency Properties get/set method question



I recently added a new dependency property to a control of mine and bound its value to my control's ViewModel.  I put custom code in the Get method of the dependency property and saw the return value from that method does not come across to the ViewModel.  After some research, it seems that when the XAML parser attempts to get or set this property's value that it does not use these get/set methods at all.  So to make this DP useful for my scenario, I have to figure out another way to set this value ahead of time, i.e. through code.

My question is, what is the point of these get/set methods for your DP if the XAML parser isn't going to use them?  Is it really all that useful to use these in code?  I would imagine DP's are mostly being used for the purpose of data binding in which case it'd be helpful to be able to define how WPF will get and set the value.  I liken this scenario to a public property with a private backing field, and having WinForms or whatever just directly access the backing field and skipping my property's getter/setter, which would obviously be a bug.

I'd appreciate any insight on this.

Get method parameter's values using reflection


By writing the following code I can get the method parameters metadata.


        void test(int x, string s)
            StackTrace st = new StackTrace(true);

            foreach (StackFrame sf in st.GetFrames())
                MethodBase mb = sf.GetMethod();
                foreach (ParameterInfo pi in mb.GetParameters())


I need the parameters value (the value that was passed to the method).

Can I get it in C#?


Thanks Gil

Executing method of the Generic class using Reflection.



I have a generic class:

 abstract public class BaseTC<T>:IExecutable where T : Core, new()
    private Type2 testInfo;
    private Type1 settings;
    private T core = null;

and I want to load such class from library the class is located in, and create an instance of it and execute methods later on.

I know how to use non-generic classes, but no idea how to call the same with generics.

Reflection question: PropertyInfo properties seem in an inconsistent state



Noticed something odd here... In class System.Collections.CollectionBase there is a property called InnerList

(see http://msdn.microsoft.com/en-us/library/system.collections.collectionbase.innerlist%28v=VS.90%29.aspx)

From the msdn link you can see that this property is read-only, as there is only a "get" and no "set". In the reflections namespace there is a class called PropertyInfo. When I query the PropertyInfo object corresponding to this InnerList property I get CanRead = true and CanWrite = false (as expected).

BUT when I go to retrieve the get method (with GetGetMethod) I get null. How is this possible, what does this mean?

Thanks in advance,

Revised Question XML Value Method Return Multiple Rows


Question earlier asked and answered to run query to collect sql data from xml schema

was able to collect the scheme from node but query did not return data but xml string


I ran this query

select x.col.value ('




cross apply



Sort Method using IComparer Interface


Hi All,

I am trying to implement sorting in list using IComparer Interface.

If any two values are equal, then there order is not maintained in the list

after using sort function.

Say, I am sorting an Employee object based on Department ID like,

Employee 1 -> Dept ID = 100, Name = "John" 

Employee 2 -> Dept ID = 100, Name = "Peter" 

Employee 3 -> Dept ID = 100, Name = "Wills" 


The output of this after sorting based on Department ID should be

100, John

100, Peter

100, Wills

but the output is

100, Wills

100, Peter

100, John

where the order is getting changed if the values in the sort column are equal.

Is it possible to maintain the order if the elements are equal.

Question about GroupPrincipal.FindByIdentity(PrincipalContext, String) Method and AD account permiss



I have an ASP.NET application that uses System.DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity (PrincipalContext, String) method to get a group principal object.  When the code runs under an AD account that has the "Read All Properties" permission for an AD global security group then the corresponding group principal object can be instantiated without problems.  However, this "Read All Properties" permission consists of fifty or so more detailed permissions to read properties like "Name", "Description", "msExchLabeledURI" and so on.  Some of these properties can be locked down for some AD accounts and I found that GroupPrincipal.FindByIdentity method may return a NULL in such cases. Is there a minimum set of group properties an AD account should have the Read permission to so the FindByIdentity method could instantiate the corresponding GroupPrincipal object?

Thanks in advance!


QUESTION: how build/create sql reports via a C# application using own user interface:


Hello all,

I would like to know if it is possible to create my own user interface, to enable me to print / create my own sql reports using the "sql reports builder". The C# user interface would simply add the various fields and parameters that one would normally add if you run the sql report builder by itself.

Is this possible? And if so could someone please point me in the right direction / site tutorials to do so/

Many thanks and kind regards





Question about filter list of objects by interface



public interface ISearchable
      int Id{ get; set;}
      int LanguageId{ get; set;}


class Programmer : ISearchable

    public int Id
            return Id;
            Id = value;

    public int LanguageId
            return LanguageId;
             LanguageId = value;
} public class Teacher : ISearchable { public int Id { get

How to get user defined properties from List in runtime using Reflection in .Net

Class Employee 
public int Sno  
     get ;  
public string Name  
     get ;  
     set ;  
        public int Salary  
    get ;  
    set ;  
class Program 
          static void Main(string [] args)  
       object r = test();  
       Type objtype = r.GetType();  
       PropertyInfo [] pi = objtype.GetProperties();  
       foreach (PropertyInfo p in pi)  
          object propertyValue = p.GetValue(r, null );  
          Console.WriteLine(":::" + propertyValue.ToString()); 
          //how to get the value of the property 
             Console .ReadLine();  
            }    //end of Main 
             private static object test()  
                List<Employee> list = new List<Employee >();  
      Employee obj = new Employee()  
                 { Name = "Ramakanth" , salary =111,Sno =10};  

                    obj = new Employee()
                 { Name = "Sravan", salary = 100, Sno = 20 };
        object objaa = list;  
        return objaa;  
Error: The following program shows the error "Target Parameter Count Exception was unhandled" 
Please help me how to rectify this error..

SVCUtil changing CallBack interface method


I am creating a WCF duplex callBack structure, with a WPF app registering with a WCF service, and the WCF service using a CallBack in the WPF app to pass data from time to time. The interface for the callback in WCF is simple:

  void DataUpdate(string updatedData)

yet when I launch the service and run SVCUtil.exe, the CS code changes the callBack interface to

  void DataUpdate(DataUpdate request)

What is the cause of this? How can I get the proper structure to show in the SVCUtil output?



Thinking about new C# method prototypes: object as dictionary

I recently had to write a small Flickr API. I know many .Net API for Flickr already exist but I needed one for a Silverlight application. Whatever, it's only about building some querystrings so I did it by myself. It's been an opportunity to think again about a classical question: how to pass parameters to a method ?

Imagine you have a generic method to call some Flickr functions.

User Defined Functions in Microsoft SQL Server

User Defined Functions are compact pieces of Transact SQL code, which can accept parameters, and return either a value, or a table. They are saved as individual work units, and are created using standard SQL commands. Data transformation and reference value retrieval are common uses for functions. LEFT, the built in function for getting the left part of a string, and GETDATE, used for obtaining the current date and time, are two examples of function use. User Defined Functions enable the developer or DBA to create functions of their own, and save them inside SQL Server.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend