gettype获取类名_避免在Type.GetType()中给出命名空间名称, - IT屋-程序员软件开发技术分享社区...

Type.GetType("TheClass");

Returns null if the namespace is not present like:

Type.GetType("SomeNamespace.TheClass"); // returns a Type object

Is there any way to avoid giving the namespace name?

解决方案

I've used a helper method that searches all loaded Assemblys for a Type matching the specified name. Even though in my code only one Type result was expected it supports multiple. I verify that only one result is returned every time I used it and suggest you do the same.

///

/// Gets a all Type instances matching the specified class name with just non-namespace qualified class name.

///

/// Name of the class sought.

/// Types that have the class name specified. They may not be in the same namespace.

public static Type[] getTypeByName(string className)

{

List returnVal = new List();

foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())

{

Type[] assemblyTypes = a.GetTypes();

for (int j = 0; j < assemblyTypes.Length; j++)

{

if (assemblyTypes[j].Name == className)

{

returnVal.Add(assemblyTypes[j]);

}

}

}

return returnVal.ToArray();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值