Click here to Skip to main content
15,883,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When implementing an interface, we have to know all its members (methods, indexes, ...) to implement without getting any errors when compiling. But how can we know that to do well? We have to refer some C# book, don't we? If so, could you please post some links here for me to download those books, they should be books of "interfaces" only.
Thank you so much!
Best Regards!
Posted

Why? When you add an interface to a class in Visual Studio, you can move the cursor into the interface name, and a little blue box appears at the left hand side of the name. Hover the mouse over this, and a drop down will appear allowing you to explicitly or implicitly implement all required methods.
 
Share this answer
 
Comments
HimanshuJoshi 31-Mar-11 15:16pm    
Better answer than mine. Have a 5.
[no name] 31-Mar-11 16:19pm    
Wow! I wish I could do it that way with my Visual C# 2008 Express Edition. It seems that you are using the professional version.
Thank you!
Henry Minute 31-Mar-11 20:03pm    
That method works with all versions of Visual Studio.

For example:
If you type public class MyClass : IComparable and leave the cursor immediately after the last 'e' you will see a little blue rectangle under the 'I' of IComparable. There are several ways to proceed from there.
1. Move your mouse pointer over the little rectangle and a down-arrow appears click that for the context menu.
2. Much quicker way. any time the little rectangle is visible hit Ctl-> (that's the Control key and the > (period) key). The context menu appears immediately.

There is another way Ctl-Alt-F10 (I think, but I may be misremembering that one).
OriginalGriff 1-Apr-11 3:26am    
Henry, my friend! Thanks for that - it's always so fiddly to get the mouse in the right place, I never knew about CTRL+.
Wish I could upvote comments: next time I see one of your answers, it gets a five!
You'll find out what they are when you haven't implemented them in your derived class.
 
Share this answer
 
Comments
[no name] 31-Mar-11 16:22pm    
With this way, I think I'll have only the names of members of the interface, but that's not enough to implement an interface.
Sergey Alexandrovich Kryukov 31-Mar-11 16:48pm    
It is a reasonable answer, my 5.
However, it all may make sense if you also reflect some real types: structures and classes.
I put a comprehensive Answer, please see.
This is not too hard to do.

You need to use System.Reflection. See this name space for more information.

Here is what you basically do:

C#
Type interfaceType = typeof(IMyInterface);
System.Reflection.BindingFlags flags =
    System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.Public |
    System.Reflection.BindingFlags.NonPublic; //very important, by default you would get only public
System.Reflection.PropertyInfo[] properties = interfaceType.GetProperties(flags);
System.Reflection.MethodInfo[] methods = interfaceType.GetMethods(flags);


As some further step, you can reflect all properties accessors as methods, for all methods reflect parameters and their type and so on.

The real question is why? John is right.
It would makes sense for class or structure. In this case you could construct the object and invoke method. It make no sense for interface. You can only instantiate any object if you have structure or class implementing this interface. The code I show may make some sense if you also reflect a class or a structure.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900