Search code examples
c#reflection.emitsystem.reflection

'System.Boolean' cannot be converted to type 'System.Reflection.RuntimePropertyInfo'


I have a class which is created dynamically .I have another existing class which has data so i am trying to map existing class data to to dynamicaly created class propeties.

But all the fileds in dynamic class are showing their types as System.Reflection.RuntimePropertyInfo.

Can anybody help me to understand why the dynamic class properties are showing as System.Reflection.RuntimePropertyInfo even though we add specified type while creating.

     public object FillData<TresultType>(TresultType result)
    {
        PropertyInfo[] pi = result.GetType().GetProperties();
        foreach (var property in pi)
        {
            var TargetProperty = this.GetType().GetProperty(property.Name);
            if (TargetProperty!=null)
            {
                TargetProperty.SetValue( this, property.GetValue(result, null), null );
            }
        }
        return this;
    }

In the above code this object is the one newly created dynamic object.The line causing problem is

    TargetProperty.SetValue( this, property.GetValue(result, null), null );

My problem is i cannot convert the existing class property type(here Boolean) to Target property type which is showing as System.Reflection.RuntimePropertyInfo

Here is my function which creates dynamic object

    public object GetViewModel<TresultType, TviewModelType>(TresultType result, TviewModelType ViewModel)
    {
        if (DynamicType.asmBuilder == null)
            DynamicType.GenerateAssemblyAndModule();
        var finalType = DynamicType.modBuilder.GetType("Beacon11");

        TypeBuilder tb = DynamicType.CreateType(DynamicType.modBuilder, ViewModel.GetType().ToString());
        tb.SetParent(typeof(ResultViewModelVersionable));

        var sourceType = result.GetType();
        var targetType = tb.GetType();

        foreach (var property in sourceType.GetProperties())
        {
            var targetProperty = targetType.GetProperty(property.Name);
            if (targetProperty == null)
            {  
                DynamicType.CreateProperty(tb, property.Name, property.GetType());
            }
        }

        finalType = tb.CreateType();
        var Methods = tb.GetMethods();
        Object obj = Activator.CreateInstance(finalType);
        return obj;
    }

This function creates model which is of type "TviewModelType" and adds the fields in "TresultType" and also data from it.

    ResultViewModelVersionable Versionable = new ResultViewModelVersionable();
        var objModel=obj.GetViewModel(vModel,Versionable);
     Type myType = vModel.GetType();
        MethodInfo magicMethod = typ.GetMethod("FillData");
        MethodInfo generic = magicMethod.MakeGenericMethod(myType);
        object magicValue = generic.Invoke(objModel,new object[]{vModel});

Please let me know if i can follow any different approch. Regards, Mohan


Solution

  • The error lies HERE:

        foreach (var property in sourceType.GetProperties())
        {
            var targetProperty = targetType.GetProperty(property.Name);
            if (targetProperty == null)
            {  
                DynamicType.CreateProperty(tb, property.Name, property.GetType());
            }
        }
    

    You are passing the CreateProperty method a type of RuntimePropertyInfo. Try THIS:

        foreach (var property in sourceType.GetProperties())
        {
            var targetProperty = targetType.GetProperty(property.Name);
            if (targetProperty == null)
            {  
                DynamicType.CreateProperty(tb, property.Name, property.PropertyType);
            }
        }
    

    Can't test this, but I suspect that should solve the problem.