App.config<绑定重定向>>对于具有强名称的程序集,然后在 C# 中使用短名称
我认为标题并没有很好地表达我的意思,所以有一个例子。
我有这个方法,它接收 System.Windows.Forms Control 的名称,然后返回类型。 (我需要使用 System.Windows.Forms 的 Version=2.0.0.0)
return Type.GetType("System.Windows.Forms." + name + ", System.Windows.Forms,Culture=neutral, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089")
我不喜欢这个方法的外观,有那个字符串看起来很奇怪。
所以我想知道是否可以在 App.config 文件中指定 System.Windows.Forms 程序集并在 c# 中使用一些短名称?
<dependentAssembly>
<assemblyIdentity name="System.Windows.Forms" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="2.0.0.0"/>
</dependentAssembly>
我所说的简称是指这样的事情:
Type.GetType("System.Windows.Forms." + name)
这可能吗?
I think Title is not very suggestive in what I meant to ask, so theres is an example.
I have this method that receives a name of System.Windows.Forms Control and then returns the type. (I need to use Version=2.0.0.0 of System.Windows.Forms)
return Type.GetType("System.Windows.Forms." + name + ", System.Windows.Forms,Culture=neutral, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089")
I don't like the appearance of this method, it seems weird by having that string.
So I was wondering if it is possible to specify System.Windows.Forms Assembly in App.config file and use some short name in c#?
<dependentAssembly>
<assemblyIdentity name="System.Windows.Forms" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="2.0.0.0"/>
</dependentAssembly>
By Short Name I mean something like this:
Type.GetType("System.Windows.Forms." + name)
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取对该程序集的引用,例如 Assembly winForms = Assembly.Load< /a> 或者LoadWithPartialName(如果您不想包含)里面有一个版本。 注意:如果您知道程序集已加载,则可以使用类似 typeof(Form) 的内容获取参考。程序集
使用 winForms。GetType("System.Windows.Forms." + name)
由于您可以从特定程序集中请求类型,因此您不需要不必给出完全限定的类型名称(包括程序集容器)
Get a reference to that assembly, like Assembly winForms = Assembly.Load or maybe LoadWithPartialName if you don't want to include a version in there. Note: if you know that assembly is already loaded, you can get a reference with something like typeof(Form).Assembly
use winForms.GetType("System.Windows.Forms." + name)
Since you're able to ask for the type from a particular assembly, you don't have to give a fully-qualified type name (including assembly container)