0

I have a parent class and several inherited from it

For some actions, I get a string with the name of the class

Then get class from this string

CommonModel model = GetModelFromString(modelType);

Then I need to create generic

Actions<T> actions = new Actions<T>();

I need replace T to model . But model is var not a class.

How can I pass the class to generic? (Is it possible at all?)

Generic

public class Actions<T> 
        where T: CommonModel
{
}

Works for me

    Type generic = typeof(Actions<>); //generic without <T>
    var model = GetModelFromString(modelType); //string model that must be in <>  

    Type[] typeArgs = { model.GetType() };
    Type constructed = generic.MakeGenericType(typeArgs);

    var myObject = Activator.CreateInstance(constructed);

    var b = (Actions<Materials>)myObject; //upcast to see methods
Anulyev
  • 143
  • 1
  • 6
  • I'll be honest and I am not sure if this is what you mean or not, but this question might be what you're after? https://stackoverflow.com/questions/2247598/how-do-i-instantiate-a-class-given-its-string-name - I won't mark as a duplicate as I'm not sure it is what you mean! – John Mar 30 '21 at 15:53
  • What is `Actions` here? it would really help if there is a non-generic base-type with a usable API, because a consumer that can't speak `T` needs *something*; I'm assuming you can map your `string` (name) to `Type` via some mechanism of your choice; constructing a generic type from a generic template and a `Type` is entirely doable, but you still need to return the new object to the caller via something they can use, hence: `Actions nonGeneric = ConstructGenericActionFromType(GetTypeFromName(name));` - if *that* would work for you, yes: that's entirely possible. – Marc Gravell Mar 30 '21 at 15:54
  • @John ,@Marc Gravell I need replace T to model . But model is var not a class. GetType() return string. I dont understand how to make a class to pass to Actions – Anulyev Mar 30 '21 at 16:01
  • I'm having trouble understanding the question, but it seems like this is something that could be solved by using System.Reflection to get the class type from model. – arc-menace Mar 30 '21 at 16:41
  • Your best bet is to "switch" on the class name and create the instances in the cases for example.... Otheriwise you will loose object orientation and have to use reflection. – Jonathan Alfaro Mar 30 '21 at 18:47
  • As @MarcGravell says, there must be some kind of base class or interface that you have, otherwise the `Actions` object is useless to you. If you have that then you can create it dynamically with reflection, and downcast it back to the base type afterwards to use in the normal fashion. Sometimes all you need is an abstract base type or an interface with a `void DoYourThing()` function, and the generic object overrides that and knows internally what the implementation should be. – Charlieface Mar 30 '21 at 23:21

1 Answers1

1

Are you mean you need something like that?

using System;

public class Program
{
    public class Actions<T> where T : CommonModel
    {
        public Actions(T model, object parameter) 
            => Console.WriteLine($"{model}", parameter);
    }

    public class CommonModel
    {
        public override string ToString() 
            => "Hello, {0}";
    }

    public static void Main()
    {
        var model = new CommonModel();
        var modelType = model.GetType();
        var actionsTemplate = typeof(Actions<>);
        var actionsOfModelType = actionsTemplate.MakeGenericType(modelType);

        Activator.CreateInstance(actionsOfModelType, model, "World");
    }
}
spzvtbg
  • 964
  • 5
  • 13
  • Tnx, it works, but there is some changes needed. I add it in the end of the question – Anulyev Mar 30 '21 at 19:00
  • Ofcourse it was a example of that how you can create a generic type from some object there also needed some type checks too ..., but i dont see why is that all if you at end knows the generic type parameter. Probably is better when you move the logic in generic method and from their returns the Actions with your wished type – spzvtbg Mar 30 '21 at 19:58