在设计时使用全名查找类的类型 [英] Find the Type of a Class using its full name during Design-Time

查看:48
本文介绍了在设计时使用全名查找类的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我正在使用如此处所述),然后单击完成,我将得到一个类似于"Namespace.ClassName" 的字符串.为了在设计器中显示所选对象的属性,我需要以优化的方式找到对象的正确 Type .

I am using DataSourceProviderService.InvokeAddNewDataSource method to display Data Source Configuration Wizard during design-time in Visual Studio. If user choose an object (as explained here), and click finish, I will get a string like "Namespace.ClassName". To display the Properties of the selected object in designer, I need to find the correct Type of the object in an optimized manner.

我有一个类的名称及其名称空间( Application.Data.Employee ).我想用此信息找到类的类型(雇员).目前,我正在使用以下代码查找类型

I have the name of a class and its namespace (Application.Data.Employee). I want to find the type of the class (Employee) with this information. At present I am using the following code to find the type

string classNameWithNameSpace = "Application.Data.Employee";
Type target;                                
foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    foreach (Type t in assembly.GetTypes())
    {
        if (t.FullName.Equals(classNameWithNameSpace))
        {
            target = t;
            break;
        }
    }

注意:项目中引用的任何dll中都可能存在程序集.我的代码还支持 .Net Framework 2.0

Note: Assembly might be present in any dll referenced in the project. My code also supports .Net Framework 2.0

由于以下原因,我知道这不是最好的方法

I know this is not the best way because of the following reasons

1)两个或更多程序集可能具有相同的名称空间名称和类名称

1) Two or more assemblies might have same namespace name and class name

2)我看到一个SO声明,它将为动态程序集抛出 NotSupportedException

2) I saw a SO post stating, it will throw NotSupportedException for dynamic assemblies

3)在调试时,发现在循环中检查了不需要或不需要的程序集中的类型. AppDomain.CurrentDomain.GetAssemblies()方法在设计时调试过程中在一个简单项目中返回146个程序集

3) On debugging found that Types in unwanted or unnecessary assemblies are checked in the loop. AppDomain.CurrentDomain.GetAssemblies() method returns 146 assemblies in a simple project during design-time debugging

4)如果以上代码将不必要的程序集加载到内存中,它将一直存在于内存中,直到存在应用程序域为止(请在此链接中检查卸载程序集部分

4) If above code loads an unnecessary assembly into memory, it will present in memory till application domain is present (check unloading an assembly section in this link https://msdn.microsoft.com/en-us/library/mt632258.aspx)

有没有推荐的方法或最佳方法来做同样的事情?

Is there any recommended way or best approach for doing the same?

推荐答案

您可以在设计时使用这些服务来处理类型:

You can use these services to work with types at design-time:

例如,您可以编写此类方法并将适当的 IServiceProvider 传递给它们,以获得这些服务:

For example you can write such methods and pass them a suitable IServiceProvider which can get those service:

Type GetTypeByName(IServiceProvider provider, string typeName)
{
    var svc= (ITypeResolutionService)provider.GetService(typeof(ITypeResolutionService));
    return svc.GetType(typeName);
}
private List<Type> GetAllTypes(IServiceProvider provider)
{
    var svc= (ITypeDiscoveryService)provider.GetService(typeof(ITypeDiscoveryService));
    return svc.GetTypes(typeof(object), true).Cast<Type>().ToList();
}

我在 TypeConverter UiTypeEditor T4 模板和附加组件中使用了这些机制.这与Visual Studio在设计时使用类型的方式相同.

I've used these mechanism in TypeConverter, UiTypeEditor, T4 templates and Add-ons. It's the same way that visual studio uses to work with types at design-time.

以下是获取属性所需的确切代码:

Here is the exact code you need to get properties:

var svc = ((DataSourceProviderService)Site.GetService(typeof(DataSourceProviderService)));
if (svc != null)
{
    var result = svc.InvokeAddNewDataSource(this, FormStartPosition.CenterScreen);
    if(result!=null && result.DataSources.Count>0)
    {
        var type = GetTypeByName(this.Site, result.DataSources[0].TypeName);
        var properties = type.GetProperties().ToList();
        MessageBox.Show(string.Join(",", properties.Select(x => x.Name)));
    }
}

这篇关于在设计时使用全名查找类的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆