0

I have a MyWorker class.

public class MyWorker<T1, T2>
{
    Dictionary<PropertyInfo, PropertyInfo> DirectionsStructs;
    Dictionary<TypeInfo, TypeInfo> DirectionsClasses;
    public MyWorker()
    {
        DirectionsStructs = new Dictionary<PropertyInfo, PropertyInfo>();
        DirectionsClasses = new Dictionary<TypeInfo, TypeInfo>();
    }

    public void AddDirection(PropertyInfo propertyInfoSource, PropertyInfo propertyInfoTarget)
    {
        DirectionsStructs.Add(propertyInfoSource, propertyInfoTarget);
    }
    public void AddDirection2(TypeInfo typeInfoSource, TypeInfo typeInfoTarget)
    {
        DirectionsClasses.Add(typeInfoSource, typeInfoTarget);
    }
    public T2 Work(T1 t1Object)
    {
        T2 TargetObject = (T2)Activator.CreateInstance(typeof(T2));
        foreach (var direction in DirectionsStructs) {/*some successfull stuff*/}
        foreach (var direction in DirectionsClasses)
        {
            var typeInfoSource = direction.Key;
            var typeInfoTarget = direction.Value;
            //below code fails
            MyWorker<typeInfoSource, typeInfoTarget> innerWorker = new MyWorker<typeInfoSource, typeInfoTarget>();
        }
        return TargetObject;
    }
}

For structs like int and string property, i use AddDirection, for concrete classes like AnotherClass, i use AddDirection2.

By the way I can get PropertyInfo and TypeInfo with expressions. For simplicity I didn't write.

I want to use recursively in Work method when it is a concrete class.

But it gives error ... is a variable but is used like a type

Previously I wanted previously AddDirection method for classes. It worked when T1 x property and T2 y property is same class. But it failed when not same class.

Then I want to use recursively MyWorker class when source and target have different classes. I don't know TypeInfo is correct. If I can get class type from PropertyInfo , that will be prettier.

But typeInfo and/or propertyInfo doesn't give me a way to create instance of MyWorker. I searched for other asked questions but I couldn't get solutions.

What should I do?

kordiseps
  • 391
  • 3
  • 12
  • 1
    Does this answer your question? [How to use local variable as a type? Compiler says "it is a variable but is used like a type"](https://stackoverflow.com/questions/32935297/how-to-use-local-variable-as-a-type-compiler-says-it-is-a-variable-but-is-used) – Pavel Anikhouski Mar 25 '20 at 11:14
  • @PavelAnikhouski Error same, but concept is different. In his solution he wanted to get value from some class. But I want to 2 different types and create instance from these 2 types then i want to access instance method. I can already access value – kordiseps Mar 25 '20 at 11:31

1 Answers1

0

I couldn't use like

var innerWorker = new MyWorker<typeInfoSource, typeInfoTarget>();

I needed to change DirectionsClasses from Dictionary<TypeInfo, TypeInfo> to Dictionary<PropertyInfo, PropertyInfo> . Then I could create instance and use method like below.

        foreach (var direction in DirectionsClasses)
        {
            Type innerMyWorkerType = typeof(MyWorker<,>);
            Type constructedMyWorker = innerMyWorkerType.MakeGenericType(direction.Key.PropertyType, direction.Value.PropertyType);
            var innerMyWorkerObject = Activator.CreateInstance(constructedMyWorker);
            var type = innerMyWorkerObject.GetType();
            var method = type.GetMethod("Work");
            var innerWorkResult = method.Invoke(innerMyWorkerObject, new object[] { direction.Key.GetValue(t1Object) });
            //Some stuff with innerWorkResult 
        }
kordiseps
  • 391
  • 3
  • 12