none
Determining the actual type of a dynamic object RRS feed

  • General discussion

  • There are many interop objects, such as in mshtml, that return the dynamic type. So we must rely on the documentation to know what type is returned by a function. Sometimes the documentation does not specify the type and sometimes the documentation is just plain wrong. So I try to determine the type at execution by debugging a test program but that often does not work.

    Given an instance of a dynamic object, how do I determine during execution what type it is? For this I don't intend to use that permanently but it would help to do that just for development purposes when the documentation does not work.

    I know that someone is going to ask for an example. Based on past experience, providing an example for something like this is more likely to distract the discussion because someone will tell me what the documentation should say about that specific object and then they will consider the question answered. I want to know how to get the answer for other objects as well, not just the one I am working with now.



    Sam Hobbs
    SimpleSamples.Info

    • Changed type Simple Samples Thursday, October 12, 2017 11:17 PM There is no answer
    Wednesday, October 11, 2017 11:08 PM

All replies

  • English is not very will, hope to be able to help you in Chinese

    法一:Not Api return,obj.GetType(),or typeof()

    法二:api return json,Use  json from nuget or in browser javascript

    法三:修改 交互源码,用ResponseData<T>泛型数据。。。再加上HelpPage,再修改xml读取接口,使会显示泛型的描述。

    暂时就想到这些吧,,,当然你也可以直接序列化、反序列化,然后转字典或模型,,来判断你正在处理的数据类型~


    Thursday, October 12, 2017 12:27 AM
  • Google translates as follows:

    Law one: Not Api return,obj.GetType(),or typeof()

    Law two: api return json,Use  json from nuget or in browser javascript

    France three: modify the interactive source, with ResponseData <T> generic data. The Plus HelpPage, and then modify the xml read interface, so that will show a generic description.

    Temporarily think of these bars ,,, of course you can also direct serialization, deserialization, and then switch to the dictionary or model, to determine the type of data you are dealing with ~

    Thank you for trying. I think Google did not translate it very well.

    Perhaps I just do not understand what you are trying to say. I think that JSON, XML, Nuget and JavaScript are not relevant here. Also serialization is not relevant.

    I am asking about the type of thing in IHTMLElement2 interface. Sometimes we only have documentation like that to look at. We add a reference for the Microsoft HTML Object Library and very many of the methods have "dynamic" for the return type. Often we don't know what type (class) the data actually is so we are unable to use the data. I am asking about the "dynamic" type and how to determine what the data actually is during execution.

    I am using a function that should be returning a string but it is not. It appears to be returning a System.Collections.HashTable but I can't get that to work either. So I don't know what type to use so that I can use the data. I think I just need to know what type to use to cast the data but I don't know how to determine that.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, October 12, 2017 1:36 AM
  • Hello Sam,

    >>Often we don't know what type (class) the data actually is so we are unable to use the data. I am asking about the "dynamic" type and how to determine what the data actually is during execution.

    If you want to get the actual type of dynamic you could choose the getType() method ,The reflect action is also suitable for dynamic type.

    static void Main(string[] args)
            {
                Assembly assembly = Assembly.LoadFrom("ClassLibrary2.dll");    
                Type[] type= assembly.GetTypes();
                Type myType  = type[0];     
                dynamic dyobject = Activator.CreateInstance(myType);          
    
                //if you wnat to Determining  the dyobject acture type, you could do as below.
                Object ob = (object)dyobject;//Convert Object type to Use intelligent prompts
                Type t=ob.GetType();
                //This will give you all property names and values defined in your dynamic variable
                foreach (PropertyInfo pr in ob.GetType().GetProperties()) {
                    Console.WriteLine(pr.Name);
                }
                foreach (MethodInfo me in ob.GetType().GetMethods()) {
                    Console.WriteLine(me.Name);
                }
    }
    

    >>So I don't know what type to use so that I can use the data. I think I just need to know what type to use to cast the data but I don't know how to determine that.

    GetType() returns the type of the wrapper for dynamic objects, If you want to real type,you could see here .

    https://stackoverflow.com/questions/7362532/get-the-type-for-a-object-declared-dynamic

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 12, 2017 11:09 AM
    Moderator
  • I tried:

    object o = dynamicobject;
    Type t = o.GetType();
    Outfile.WriteLine("type: {0}", t.Name);
    

    I got:

    type: __ComObject

    I am expecting a string type but casting to string type does not work. There is an object but I see nothing telling me what the type is. I looked at the "t" object during a break and I see nothing that helps.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, October 12, 2017 5:51 PM
  • I changed th4e thread type to Discussion. I think there is no answer. Microsoft is either unable or unwilling to provide this type of thing for many COM objects. Of course they want us to use .Net and then we are able to determine the type and new stuff, such as Edge, will use .Net.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, October 12, 2017 11:20 PM
  • Apparently Microsoft.VisualBasic.Information.TypeName does get a type name even when GetType just gets "__ComObject". If there is a method that does that without using Microsoft.VisualBasic then that would help.


    Sam Hobbs
    SimpleSamples.Info

    Friday, October 13, 2017 5:41 PM
  • it's a com object?

    you can search the keyword like 

    MarshalAs ExWebbrowser

    Or use a DE tools show the microsoft .net lib Source Code

    the lib Source Code is Best teacher

    I Writed A MyWebBrowser like ExWebbrowser,but not in this pc.

    the source code hav use com interface

    may be ,you can try search kerword like "sink"

    I forgot the specific words, want to reinforce the great god

    I hope my own translation, can better than Google translate


    Saturday, October 14, 2017 12:20 AM