views:

82

answers:

1

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  A: 
  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)

James Manning
I need Type to create a new Instance, "name" could be Form, Panel, TextBox, etc.By doing this -> typeof(Form).Assembly I be able to instantiate any Control From System.Windows.Firms?Thanks
well, that's different than the question you asked ("then returns the type") but you could either fetch the no-args ctor and create it using [GetConstructor](http://msdn.microsoft.com/en-us/library/h93ya84h.aspx) or (simpler) just call [Activator.CreateInstance](http://msdn.microsoft.com/en-us/library/wccyzw83.aspx) on the type
James Manning