Partilhar via


COM and Native Interop for .NET

I decided to post some of the questions we received from user's to my blog, read on for those interested... Also, the names of the people have been removed to protect the innocent....

Question: what happens with interop and optional arguments for mthod calls? Are they still optional? I recall reading something but cannot remember exactly how it worked.

Answered: Type.InvokeMember can be used to invoke methods with parameters that have default values. To bind to these methods, Reflection requires BindingFlags.OptionalParamBinding to be specified. For a parameter that has a default value, you can either supply a different value, or supply Missing.Value to use the default value. For example, consider a method such as MyMethod(int x, float y = 2.0). To invoke this method with only the first argument as MyMethod(4), pass one of the above binding flags and pass two arguments, namely, 4 for the first argument and Missing.Value for the second argument. Unless you use Missing.Value, you may not omit optional parameters with the Invoke method. If you must do so, use InvokeMember instead. https://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemTypeClassInvokeMemberTopic.asp


Question : How to use C# code COM?

Answer: You need to compile the code into an assembly, and then create a COM-Callable Wrapper for it, for COM to see the C#-coded assembly. VS.NET can create this for you, or you can use a combination of TLBEXP and REGASM to do this.

Comments

  • Anonymous
    February 22, 2004
    Hi Adam,
    It's also worthy to note that VB.NET and C# handle optional parameters differently: VB.NET handles them like you'd expect and C#, well, not at all.

    We encounter this frequently when we program against the SharePoint Portal Server 2001 object model. We prefer to use C# for development but in the case of SPS2001, as an example, we use VB.NET.

    Cheers,
    Michael.