C# 知道类型名称 如何通过类型名称得到type。

youlanse 2010-07-26 06:22:54
Type myType = System.Type.GetType("System.Drawing.Color",true);

一般都int bool 类型都可以
color 的时候有异常 请问怎么解决!

System.Type.GetType("System.Drawing.Color", true) “System.Type.GetType("System.Drawing.Color", true)”引发了“System.TypeLoadException”类型的异常 System.Type {System.TypeLoadException}
...全文
2203 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
iammac 2010-07-27
  • 打赏
  • 举报
回复
那可能要定义一个列表吧,例如

static Dictionary<string, string> asmDicts = new Dictionary<string, string>()
{
{"System.Drawing",@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll"}
};

然后用的时候

string typeStr = "System.Drawing.Color";
string asmStr=null;
Type myType;
int index = typeStr.LastIndexOf('.');
if (index > -1)
asmStr = typeStr.Substring(0, index);
var asm = Assembly.LoadFile(asmDicts[asmStr]);
if(asm!=null)
myType = asm.GetType();
youlanse 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 iammac 的回复:]
不能这样吗?

C# code

private Type myBehaviorType = null;
public Type behaviorType
{
get { return myBehaviorType; }
set { myBehaviorType = value; }
}


用的时候

C# code

this.behavi……
[/Quote]



我在设置属性behaviorType的时候 只知道Type的string 随意只能设置string
iammac 2010-07-27
  • 打赏
  • 举报
回复
不能这样吗?

private Type myBehaviorType = null;
public Type behaviorType
{
get { return myBehaviorType; }
set { myBehaviorType = value; }
}

用的时候

this.behaviorType = typeof(T);
youlanse 2010-07-27
  • 打赏
  • 举报
回复
但是 如果每个都要手动去指定那不是很麻烦,而其以后遇到别的类型也要在手动制动啊,没有自动的什么方法吗?
我在类头文件中 添加 using System.Drawing;的引用也不行!
zcfzcf001 2010-07-27
  • 打赏
  • 举报
回复
构造时候指定,或者在public string behaviorType内指定不也行码?
youlanse 2010-07-27
  • 打赏
  • 举报
回复
代码是这样的
myBehaviorType 是类中的一个属性
初始为null 通过set typeName 得到type
需要在什么地方指定程序集? 还有其他方法吗?

private Type myBehaviorType = null;
public string behaviorType
{
get { return myBehaviorType.ToString(); }
set { myBehaviorType = System.Type.GetType(value, true);}
}


leiziaitudou 2010-07-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iammac 的回复:]
除非是当前程序集或是mscorlib,不然需要指定程序集

C# code

var asm = Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll");
Type myType = asm.GetType("System.……
[/Quote]
dddd
showjancn 2010-07-26
  • 打赏
  • 举报
回复
枚举。
chuzhaowei 2010-07-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mayonglong 的回复:]

引用 1 楼 iammac 的回复:
除非是当前程序集或是mscorlib,不然需要指定程序集

C# code

var asm = Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll");
Type myType = asm.GetType("System.……
……
[/Quote]

呵呵
mayonglong 2010-07-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iammac 的回复:]
除非是当前程序集或是mscorlib,不然需要指定程序集

C# code

var asm = Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll");
Type myType = asm.GetType("System.……
[/Quote]

正解~~
skyworth98 2010-07-26
  • 打赏
  • 举报
回复
sp

[Quote=引用 1 楼 iammac 的回复:]
除非是当前程序集或是mscorlib,不然需要指定程序集

C# code

var asm = Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll");
Type myType = asm.GetType("System.……
[/Quote]
iammac 2010-07-26
  • 打赏
  • 举报
回复
除非是当前程序集或是mscorlib,不然需要指定程序集

var asm = Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll");
Type myType = asm.GetType("System.Drawing.Color");

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧