I am struggling to inject a class and its subclass properties from source to target.
For instance, I am getting objects like below from the return value of a method. I need to inject it into a target type.
Class A
{
prop A
prop B
}
Class B : A
{
prop C
prop D
}
I wrote the below code for array and value checking, but couldn't get an idea on how to write for class i.e "IsClass". No where in internet I got an idea as my googling says they have for IsGenericType.
Please help me
protected override object SetValue(ConventionInfo c)
{
if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string)
|| c.TargetProp.Type.IsValueType || c.TargetProp.Type == typeof(string))
return c.SourceProp.Value;
if (c.SourceProp.Type.IsArray)
{
var arr = c.SourceProp.Value as Array;
if (arr != null)
{
var clone = Activator.CreateInstance(c.TargetProp.Type, arr.Length) as Array;
for (int index = 0; index < arr.Length; index++)
{
var a = arr.GetValue(index);
if (a.GetType().IsValueType || a is string) continue;
if (clone != null)
clone.SetValue(Activator.CreateInstance(c.TargetProp.Type.GetElementType()).InjectFrom<CloneInjection>(a), index);
}
return clone;
}
}
**If c.SourceProp.Type.IsClass Then
Return Activator.CreateInstance(c.SourceProp.Type).InjectFrom(Of CloneInjection)(c.SourceProp.Value)
End If**
return Activator.CreateInstance(c.TargetProp.Type)
.InjectFrom<CloneInjection>(c.SourceProp.Value);
}
UPDATE
I added the last IsClass condition. Still same exception that I used to get without adding it.
An exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll but was not handled in user code.
Additional information: Public member 'InjectFrom' on type 'B' not found.