views:

36

answers:

1
  public class MyController
    {
        public object CreateByEnum(DataModelType modeltype)
        {
            string enumText = modeltype.ToString(); // will return for example "Company"
            Type classType = Type.GetType(enumText); // the Type for Company class
            object t = Activator.CreateInstance(classType); // create an instance of Company class
            return t;
        }
    }

    public class CompanyView
    {
        public static List<Personel> GetPersonel()
        {
            MyController controller = new MyController();
            _Company company = controller.CreateByEnum(DataModelType.Company) as _Company;
            return company.GetPersonel();
        }
    }

    public enum DataModelType
    {
        xyz,
        klm,
        tucyz,
        Company
    }

Yes, I agree Activator.CreateInstance() is very useful. Unfortunately, I need to pass in the correct type. That means building the correct string to pass to Type.GetType(). If I trace through the call to Controller.CreatebyEnum() in the code I posted above, simply doing modelType.ToString() isn't sufficient, even for the case of DataModelType.Company. My solution'll be maintenance bottleneck. What would be better is something that takes the results of modelType.ToString() and then recursively searches through all the types found in all the assemblies loaded in the current AppDomain. According to MSDN, Type.GetType() only searches the current calling assembly, and mscorlib.dll. How can i do that? . i need best performance?

A: 

I use Activator.CreateInstance as a core part of my framework, mainly to instansiate implementations of my data access providers (which are defined by an interface).

Because I know which one I want, I simply pass in an AppSetting key - so you can change which ones you use via config.

I agree with Kane - I'm not 100% sure what your question is.

If you're passing in an enum, doesn't that kind of suggest you know what type it is you want to create (even if only in a general sense, and not an explicit one)?

At some point something is going to have to make a decision as to what is instansiated - do you know who/what that is? Possible options:

  1. Via config: good for when you need control but don't actually change the settings too often. Also good for broad concepts (Clients = Setting A; Products = Setting B, etc).
  2. Store the target type in the database with related data - would allow you to bind a specific implementation (to be passed into the activator) with any class instance. This allows a finer degree of control - but how is that type decided on and stored in the first place?
  3. Rather than use ToString() (which you could overload/replace) use OO to add a new member on to each class in question (perhaps a simple interface), where the new member returns the information needed for the activator.

It might simply be that you're asking too much of the enum itself? You should still be able to use the enum to control execution, but you need to seperate out how instances are created.

Is that clear enough, or do people want / need code examples?

Adrian K