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?