Get type in referenced assembly by supplying class name as string?

后端 未结 2 895
南方客
南方客 2020-12-29 12:07

These are similar questions: How-to: Load a type from a referenced assembly at runtime using a string in Silverlight, GetType on a class in a referenced assembly fails but n

相关标签:
2条回答
  • 2020-12-29 12:40

    For the first question, you could do something like

    Type t = AppDomain.CurrentDomain.GetAssemblies()
                                    .Where(a => a.FullName == "MyFramework")
                                    .SelectMany(a => a.GetTypes())
                                    .Where(t => t.Name == "Car")
                                    .FirstOrDefault();
    

    I am not sure what you mean by the second question.

    0 讨论(0)
  • 2020-12-29 12:49

    Maybe the referenced assembly isn't loaded at the time. Also, I understand from your question that you do not have the full type name, only the class name.
    You should try something along this line then:

    Type type = Assembly.Load("YourAssemblyName").GetTypes().First(t => t.Name == "ShortTypeName");
    

    Hope I understood you correctly.

    0 讨论(0)
提交回复
热议问题