0

I am new to WPF

By using this command GetInstance("HSProModel.CountryMaster") i am getting the class CountryMaster , actually i am getting the name of user control & namespace from the database and trying to open the corresponding usercontrols in a tab.

Instead of casting individual types how can i cast it dependends on the type returned by GetInstance("HSProModel.CountryMaster") ? Any help please.

CountryMaster obj = (CountryMaster) GetInstance("HSProModel.CountryMaster") ; // It works

ProvinceMaster obj = (ProvinceMaster) GetInstance("HSProModel.ProvinceMaster") ; // It works

My purpose is to make an instance of the CountryMaster like new CountryMaster()

Murali K
  • 23
  • 4
  • 1
    I am afraid, I could not quite understand the problem your are facing. If you want to use the new keyword and create an object, you can do so by referencing the assembly in which the required type resides. – Durga Prasad Jul 05 '20 at 14:33

1 Answers1

0

If you know that all type names in the database refer to UserControls, you could always cast to UserControl:

string dynamicName = "HSProModel.CountryMaster";
UserControl userControl = (UserControl) GetInstance(dynamicName) ;

This is how "generic" it gets unless all types share some other common base class or interface.

If you want to create an CountryMaster without using reflection, you could check the value of the string an new it up as usual:

if (dynamicString.EndsWith("CountryMaster"))
    CountryMaster obj = new CountryMaster();
mm8
  • 163,881
  • 10
  • 57
  • 88