0

In my application, I'm using a package that reads a specific type of file (let's say .custom) that describes a house. It has custom types to describe objects (t_doors and t_windows to keep it simple)

This package has a function Reader.GetAll<t_type>() that gives me all the objects of said type in the file, and a Reader.GetItem<t_type>(int itemId) that gives me a specific t_type object with itemId ID.

Now i'm reading a list of required updates from a user-provided file that includes the type, the id and the changes in the form of a List<List<string>>. This part is outside my control.

I need to be able to read the type from the string and pass it to my generic function.

So far I've tried :

var r = new List<string>(){ "t_door", "1", "Color:Red" };
Type T = Type.GetType(r[0]);
ModifyProperties<T>(r[1], r[2]);

'T' is a variable but is used like a type.

Same error with:

ModifyProperties<typeof(T)>(r[1], r[2]);

And :

ModifyProperties<T.GetType()>(r[1], r[2]);

Operator '<' cannot be applied to operands of type 'method groups' and 'Type' 

Here is the signature of the ModifyProperties function :

public static void ModifyProperties<ObjectTypeToEdit>(string id, string modification) where ObjectTypeToEdit : t_object

So far my workaround has been to use a switch on the type and call the method with the correct type :

switch (r[0])
{
    case "t_door":
        ModifyProperties<t_door>(r[1], r[2]);
        break;
    case "t_window":
        ModifyProperties<t_window>(r[1], r[2]);
        break;
    ...
}

Which is far from ideal considering there are hundreds of types of objects that could possibly be required.

Also i read something about an Activator class but i'm not sure how it would help.

Same with reflections :

object obj = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(r[0]);
René Vogt
  • 43,056
  • 14
  • 77
  • 99
ThePainfull
  • 125
  • 6
  • might [this post](https://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class) help? – Mong Zhu Jan 21 '20 at 13:47
  • Look for `MethodInfo.MakeGenericMethod(Type t)` or `Type.MakeGenericType()`. Within the `<>` of a generic method you can only provide types at compile-time. – René Vogt Jan 21 '20 at 13:47
  • probably [this](https://stackoverflow.com/a/16699405/5174469) would be a fitting duplicate – Mong Zhu Jan 21 '20 at 13:48
  • Did you write `ModifyProperties` or is it a method provided by the framework? If you wrote it, can you show its implementation? – Sweeper Jan 21 '20 at 13:48
  • 1
    The duplicate should answer your question of how to call it. It looks like an interesting design though. Why is `ModifyProperties()` a generic method if it doesn't require parameters of type `T` nor returns a `T` instance? – CodeCaster Jan 21 '20 at 13:49
  • @sweeper it is pre-implemented – ThePainfull Jan 21 '20 at 13:56
  • @MongZhu I'm looking at these, it might take me a while to understand but it seems to be what i'm asking. Thanks. – ThePainfull Jan 21 '20 at 13:57
  • @CodeCaster I'm really not sure why, sorry – ThePainfull Jan 21 '20 at 14:00
  • @CodeCaster The generic parameter really just acts as another `Type` parameter here, no? For me, I prefer the aesthetics of passing in `Type` parameters as a generic argument, though you can't do it when you don't know the type at compile time... – Sweeper Jan 21 '20 at 14:06
  • @Sweeper I'm wondering _why_ `ModifyProperties()` requires the type info, if all it does is more reflection magic using strings, and it apparently internally already has the collection of objects (given they need to pass an `id` to look it up, as opposed to the actual object itself). They do need something to call `GetProperty` on though. – CodeCaster Jan 21 '20 at 14:11

0 Answers0