-1

I am accessing a COM object and a method returns a dynamic variable. I do not have the implementation of the method that returns the dynamic variable and I need to cast it to the appropriate type so that I can use it in my class.

So I would like to know if there is a way to find the underlying type of a dynamic variable during runtime.

The dynamic variable is the value returning from a COM function so the UnWrap doesnt work and GetType() returns COMObject type.

thanks

Vicky-Torres
  • 219
  • 2
  • 8
  • 1
    Why do you need to cast it? Just make the parameter of your method the type you would normally cast it to and pass it in. The runtime will take care of the rest. – Asad Saeeduddin Dec 30 '14 at 06:47
  • A good way to help you discover the type of object, is to use your debugger. Put a break-point on the line after the method call, and use the Watch window to inspect the returned object. – Mohammad Dehghan Dec 30 '14 at 06:48
  • 1
    Putting it another way, pretend the `dynamic` reference you have is already of the type you need it to be, and write your code accordingly. It would be easier to demonstrate what I mean if you showed us some code. – Asad Saeeduddin Dec 30 '14 at 06:50

1 Answers1

0

If the object being returned can be one of many Types then you're best to keep using it as a dynamic and only trying to access methods and properties you know will exist (I'd expect the COM method to have some indication of how to use the dynamic it returns).

var canBeLiterallyAnything = ComMethod();
canbeLiterallyAnything.MethodDocsSayExists();
var propVal = canBeLiterallyAnything.SomeProperty;

Of course, if all of the possible Types all implement the same interface, you could cast to that interface.

var typeSafeReference = (ISharedInterface)canBeLiterallyAnything;

If you know that the COM method returns a specific Type but just don't know what that Type is then for the purpose of investigation you can call GetType() and either write it to console or set a breakpoint and inspect it. This will let you then update your code to include a cast to that Type (which would minimise the impact of the use of dynamic, but also introduce the risk of a bad cast if other Types can be returned).

var type = canBeLiterallyAnything.GetType();

// e.g. If the above returns a Type of 'SpecificType', then you can update code to
var typeSafeReference = (SpecificType)canBeLiterallyAnything;

It should be noted that the COM method might not return a concrete Type, it might be returning an anonymous object, in which case there is no casting you can do so you'll have to just keep using it as a dynamic and only access properties/methods you know exist.

Steve Lillis
  • 3,263
  • 5
  • 22
  • 41
  • When accessing through watch or getting the type, the type of the variable is COMObject. So I dont have a clue as to which type to cast the variable to. Also I am not able to check the implementation of the COM method, so cannot find the type of the returning object. – Vicky-Torres Jan 01 '15 at 06:03