1

I have 100 classes all having similar signature methods just their internal definition is different . I will be passing one of class name as string and I need to create object of that type and call methods . I know I can use "Activator.CreateInstance" but not sure exactly how in this case . For example lets say I have classes named Calculator1 to Calculator100 I am not sure how to cast it in second line depending on Class name string .

  ObjectHandle handle = Activator.CreateInstance("NameSpaceCalculator", "Calculator1");
  var Calculator = (Calculator1)handle.Unwrap();
9
  • 1
    Use a common interface. Commented Aug 27, 2013 at 0:49
  • You won't cast it. Unless these classes all share an interface, casting doesn't even make sense. What exactly are you trying to do? Commented Aug 27, 2013 at 0:49
  • You can't do so, unless touching some kind of dynamic. However you can still use Reflection to access the created object's members without having to cast it to the desired type. Commented Aug 27, 2013 at 0:49
  • Do they all share a BaseClass or Interface? Commented Aug 27, 2013 at 0:51
  • @sa_ddam213 Yes they share a common base class , not interface . Commented Aug 27, 2013 at 0:55

3 Answers 3

1

This works for me, but an Interface would be the better approach for this:

void Main()
{
    var x = CreateClass("A");
    x.Unwrap();
}

public BaseC CreateClass(string typeName)
{
    var type = Type.GetType(string.Format("MyNamespace.{0}",typeName);
    return (BaseC)Activator.CreateInstance(type);
}


public class A : BaseC
{
    public override void Unwrap()
    {
        Console.WriteLine("A");
    }
}

public class B : BaseC
{
    public override void Unwrap()
    {
        Console.WriteLine("B");
    }
}

public class BaseC
{
    public virtual void Unwrap()
    {
        Console.WriteLine("BaseC");
    }
}


//// Here is the approach with an Interface (note that overrides and virtual declaration needed. 





void Main()
    {
        var x = CreateClass<IBase>("MyNamespace","A");
        x.Unwrap();
    }

    public T CreateClass<T>(string classNamespace, string typeName) where T : class
    {
        var type = Type.GetType(string.Format("{0}.{1}",classNamespace, typeName));
        return (T)Activator.CreateInstance(type);
    }


    public class A : IBase
    {
        public void Unwrap()
        {
            Console.WriteLine("A");
        }
    }

    public class B : IBase
    {
        public void Unwrap()
        {
            Console.WriteLine("B");
        }
    }

    public interface IBase
    {
        void Unwrap();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I used the CreateClass approach from Saddam. It works perfectly but in your Baseclass the Method has to be virtual, so the child method is called.
I postet an approach using an Interface too. Its a lot better because you dont need overrides and virtual methods.
1

You could either use a common interface with an Unwrap() method defined and cast to that interface, or you could use reflection to find the Unwrap method and call it.

Unless you're loading the assembly at runtime and can't use a compile time referenced interface in it, I'd use the first option as reflection is slow and overcomplicated for this case.

4 Comments

C# reflection is actually quite fast.
UnWrap() is part of ObjectHandle not his Calculator classes, I'm not to sure why there is a cast to ObjectHandle in the first plave
@sa_ddam213 I see now. I still think writing a common interface will be the best solution though.
@JanDoerrenhaus It depends on the definition of 'slow'. Calling may be quick, but creating the method/property/etc objects isn't fast.
1

Perhap you could get the correct type first the create the instance.

public CalculatorBase CreateClass(string typeName)
{
    var type = Type.GetType(string.Format("NameSpaceCalculator.{0}",typeName));
    return (CalculatorBase)Activator.CreateInstance(type);
}

1 Comment

I tried this but it calls CalculatorBase class methods instead of that particular class .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.