开发者

App.config <bindingRedirect/> for assembly with Strong Name and then use Short Name in C#

开发者 https://www.devze.com 2023-01-07 05:13 出处:网络
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

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>
  <assemblyId开发者_C百科entity 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?


  1. 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

  2. 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)

0

精彩评论

暂无评论...
验证码 换一张
取 消