Ask a questionAsk a question
 

AnswerActivator and generic interface problem

  • Thursday, November 05, 2009 9:13 PMPrometheusMS Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a generic interface:

    interface ITest<T>
    {
            T ExecutionResult { get; set; }
           
            void Init();

            T Execute();
    }

    also i have few classes:

    public class TestClass1: ITest<TestResponse1>
    {
    // ITest methods
    ....
    }

    public class TestClass2: ITest<TestResponse2>
    {
    // ITest methods
    ....
    }


    Now in the code I have method which says:


    //type is Type of TestClass1 or TestClass2
    public void Start(Type type)
    {
       Type basicType = Type.GetType(test.Type);
       Type genericType = basicType.BaseType.GetGenericArguments()[0];
       // and I need to do something like this:
       ITest<genericType> obj = (ITest<genericType>)Activator.CreateInstance(basicType);
       //problem is this genericType
    }


    Is anyone who knows how to implement this Activator?

    Thanks

Answers

  • Thursday, November 05, 2009 10:41 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Take a look at the FormFactory class in this thread.

    You will need to cast your instance object to an inherited type.
    Like a concrete base class, an abstract class, or even better an interface.

    "Code to composition, not inheritance."  You are coding to inheritance.

    Rudy   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."

All Replies

  • Thursday, November 05, 2009 10:41 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Take a look at the FormFactory class in this thread.

    You will need to cast your instance object to an inherited type.
    Like a concrete base class, an abstract class, or even better an interface.

    "Code to composition, not inheritance."  You are coding to inheritance.

    Rudy   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."