Activator Issues .CreateInstance ()

I have a factory that needs to create objects that inherit from the Foo class at runtime. I think the return type of System.Activator.CreateInstance was the same as the type of the object being created, but judging by the following error message, its return type is Object.

Error 1: It is not possible to implicitly convert the type 'object' to 'cs_sandbox.Foo'. Explicit conversion exists (are you skipping listing?) F: \ projects \ cs_sandbox \ Form1.cs 46 24 cs_sandbox

Ok, maybe I miss the role, but

return (t)System.Activator.CreateInstance(t);

leads to another error message, which, I must admit, makes no sense to me:

Error 1: Could not find the name of the type or namespace 't' (do you miss the using directive or assembly references?) F: \ projects \ cs_sandbox \ Form1.cs 45 25 cs_sandbox

And here is my code:

class Foo { }
class FooChild1 : Foo { }
class FooChild2 : Foo { }

class MyFactory
{
    public static Foo CreateInstance(string s)
    {
        Type t;
        if (s.StartsWith("abcdef"))
        {
            t = typeof(FooChild1);
            return System.Activator.CreateInstance(t);
        }
        else
        {
            t = typeof(FooChild2);
            return System.Activator.CreateInstance(t);
        }
    }
}

How can I fix this code? Or, if it is not fixed, what are the other ways to create objects that inherit from a particular class at runtime?

+3
source share
6 answers

You need to pass the returned object to the type Foo. It makes no sense to cast it to the type defined in the variable. It must be known to the compiler, since the entire casting point through the inheritance hierarchy satisfies the check of the static type of the compiler.

return (Foo)System.Activator.CreateInstance(t);

System.Activator.CreateInstance<T>, ( , , ):

return System.Activator.CreateInstance<FooChild1>();
+11

UnWrap() , MSDN:

// Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
ObjectHandle obj = domain.CreateInstance("ObjectHandleAssembly", "MyType");

// Unwrapps the proxy to the MyType object created in the other AppDomain.
MyType testObj = (MyType)obj.Unwrap();

MSDN. , :

    ObjectHandle obj = domain.CreateInstance("Other.Assembly.Name", "Full.Class.Namespace.ClassName");
+2

Foo

return System.Activator.CreateInstance(t) as Foo;

return (Foo)System.Activator.CreateInstance(t);

, , . null. , .

?

public static OutType CreateInstance<OutType>(string s)
{
    Type t;
    if (s.StartsWith("abcdef"))
    {
        t = typeof(Bar);
        return System.Activator.CreateInstance(t) as OutType;
    }
    else
    {
        t = typeof(Meh);
        return System.Activator.CreateInstance(t) as OutType;
    }
}

. Bar Meh, Foo. , ( ).

, , . a >

+1

, , , :

public static Foo CreateInstance(string objectIdentifer)
{
   if (objectIdentifier == "Child1")
   {
      return (Foo)Activator.CreateInstance("Foo.FooChild1, FooChild1");
   }
   else
   {
      return (Foo)Activator.CreateInstance("Foo.FooChild1, FooChild2");
   }
}

, CreateInstance , FooChild1 FooChild2. , FooChild1 FooChild2, , .

?

0
source

I consider it correct:

public static Foo CreateInstance(string objectIdentifer)
{
   if (objectIdentifier == "Child1")
   {
      return (Foo)Activator.CreateInstance(Type.GetType("Foo.FooChild1, FooChild1"));
   }
   else
   {
      return (Foo)Activator.CreateInstance(Type.GetType("Foo.FooChild1, FooChild2"));
   }
}

Hope this helps you

0
source

Maybe a little late, but if I don't know the type (in case you create your own code coder), I use Convert.ChangeType

  string original = "1";
  object value;
  value = Convert.ChangeType(original, typeof(int));

typeof (int) can be any type that you don't know using object.GetType ();

0
source

Source: https://habr.com/ru/post/1718655/


All Articles