简体   繁体   中英

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

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?

  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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM