.NET Forum / .NET Framework / Interop / August 2004
Type.GetMethod returns null?
|
|
Thread rating:  |
Rory Plaire - 17 Aug 2004 20:56 GMT Greetings,
Perhaps someone has seen this and can give me a clue:
I'm loading a COM Automation Object (MSAccess) with late binding to avoid having to deal with multiple versions.
Here is the code: //-----------------------------begin code Type accessType = Type.GetTypeFromCLSID(new Guid("{73A4C9C1-D68D-11D0- 98BF-00A0C90DC8D9}"), true);
System.Security.Permissions.ReflectionPermission permission = new System.Security.Permissions.ReflectionPermission( System.Security.Permissions.ReflectionPermissionFlag.MemberAccess) ;
permission.Demand();
MethodInfo newCurrentDatabase = accessType.GetMethod ("NewCurrentDatabase", new Type[] { typeof(string) } ); MethodInfo openCurrentDatabase = accessType.GetMethod ("OpenCurrentDatabase"); MethodInfo currentDb = accessType.GetMethod("CurrentDb"); MethodInfo closeCurrentDatabase = accessType.GetMethod ("CloseCurrentDatabase"); MethodInfo quit = accessType.GetMethod("Quit"); ... //------------------------------end code
I've tried using Type.GetTypeFromProgId() as well. For some reason the GetMethod method always returns null on machines with Access 2000, but works well on my development machine with Access 2003.
Any ideas why GetMethod returns null?
thanks, -rory
Mattias Sj?gren - 17 Aug 2004 22:02 GMT Rory,
>Any ideas why GetMethod returns null? Probably because no type information is available. A PIA for the COM library must be registered for the metadata to be found.
Mattias
 Signature Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Rory Plaire - 17 Aug 2004 22:31 GMT > Probably because no type information is available. A PIA for the COM > library must be registered for the metadata to be found. > > Mattias Thanks for the reply, Mattias,
It's Office 2000, so I've had to create an interop assembly. However, this seems to be fragile and not work on everyone's machine (gives me a "QueryInterface failed..." sometimes).
I thought that COM interop could extract the type information from the TypeLib at runtime with Type.GetMethod if I do late binding... what you are saying is that this is not true?
-r 8)
Mattias Sj?gren - 18 Aug 2004 09:11 GMT Rory,
>I thought that COM interop could extract the type information from the >TypeLib at runtime with Type.GetMethod if I do late binding... what you are >saying is that this is not true? Yes, Reflection only understands managed metadata, not COM type info. So to reflect on a COM library you need an interop assembly describing the types.
Mattias
 Signature Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Robert Jordan - 18 Aug 2004 12:40 GMT hi matthias,
> Rory, > [quoted text clipped - 5 lines] > So to reflect on a COM library you need an interop assembly describing > the types. are you sure? we definitely *call* methods and properties of COM-objects w/out any interop library:
object comObj; // some type-less COM-object InvokeMethod(object, "methodName", ...);
public static object InvokeMethod(object target, string methodName, params object[] args) { return target.GetType().InvokeMember( methodName, BindingFlags.InvokeMethod, null, target, args ); }
of course, metadata cannot be *queried* that way w/out an interop wrapper, but you always can call the COM-object's members via reflection.
bye rob
Mattias Sj?gren - 19 Aug 2004 11:02 GMT Robert,
>of course, metadata cannot be *queried* that way w/out >an interop wrapper, but you always can call the COM-object's >members via reflection. Right, that's what I meant, thanks for clarifying.
Mattias
 Signature Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Robert Jordan - 18 Aug 2004 12:46 GMT hi rory,
> MethodInfo newCurrentDatabase = accessType.GetMethod > ("NewCurrentDatabase", new Type[] { typeof(string) } ); [quoted text clipped - 12 lines] > > Any ideas why GetMethod returns null? you don't need to query the metadata (that doesn't work w/out an interop wrapper) to be able to invoke methods. just use this pattern:
object result = InvokeMethod(accessObject, "NewCurrentDatabase", "some string") ...
public static object InvokeMethod(object target, string methodName, params object[] args) { return target.GetType().InvokeMember( methodName, BindingFlags.InvokeMethod, null, target, args ); }
bye rob
Rory Plaire - 18 Aug 2004 23:41 GMT Robert Jordan <robertj@gmx.net> wrote in news:cfvfj0$h4b$03$1@news.t- online.com:
> you don't need to query the metadata (that doesn't work w/out > an interop wrapper) to be able to invoke methods. Cool, thanks Rob...
Just for the record, does InvokeMember used this way go through IDispatch? If so, can it be used for COM objects which don't implement IDispatch?
thanks, -r 8)
Robert Jordan - 19 Aug 2004 14:27 GMT hi rory,
>>you don't need to query the metadata (that doesn't work w/out >>an interop wrapper) to be able to invoke methods. > > Cool, thanks Rob... you're welcome!
> Just for the record, does InvokeMember used this way go through IDispatch? > If so, can it be used for COM objects which don't implement IDispatch? no, if you don't use/have an interop assembly for the COM type then automation (IDispatch) is the only way to invoke something.
bye rob
Rory Plaire - 20 Aug 2004 00:52 GMT > no, if you don't use/have an interop assembly for the COM type > then automation (IDispatch) is the only way to invoke something. Thank you both for helping me penetrate the subtlties of COM interop! The InvokeMember with Method binding did the trick, and now I understand how interop assemblies are used in binding...
appreciative, -rory 8)
Free MagazinesGet 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 ...
|
|
|