Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2004

Tip: Looking for answers? Try searching our database.

Reflection: InvokeMember and GetProperty/SetProperty

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick Strahl [MVP] - 02 Jul 2004 08:58 GMT
A while back I've built a bunch of generic Reflection routines that simplify
the process of access properties/fields and methods a bit easier by creating
wrappers around Property/Field/Method info members.

For example:

public const BindingFlags MemberAccess =

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase;

public static object GetProperty(object Object,string Property)

{

return Object.GetType().GetProperty(Property,wwUtils.MemberAccess |
BindingFlags.GetProperty).GetValue(Object,null);

}

There are versions for Get/Set and fields and properties as well as versions
that can nest references. All works fine with .Net types.

Today I tried to use this same code with a COM object and promptly it fell
flat. In the above example, GetProperty() fails to find the property.
However, the following works:

return Object.GetType().InvokeMember(Property,wwUtils.MemberAccess |
BindingFlags.GetProperty,null,Object,null);

So now I'm wondering what is InvokeMember doing that is different here? I
seem to remember a discussion here that InvokeMember is a bit less
efficient, so if possible I'd like to avoid this.

Any ideas if it's possible to get the code to run with GetProperty et al.?

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web

Mattias Sj?gren - 16 Jul 2004 21:52 GMT
Rick,

>So now I'm wondering what is InvokeMember doing that is different here?

For COM objects it calls the property or method using IDispatch
(GetIDsOfNames followed by Invoke).

However, GetProperty and all the other reflection methods only
understand managed metadata, so you need an interop assembly
describing the COM API for them to work as expected.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.