-1

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

Chandra Mohan
  • 729
  • 2
  • 10
  • 29
  • In what line does the error occur? What is TargetProperty.PropertyType at runtime? – usr Jun 08 '14 at 12:01
  • The following line throwing excetion "TargetProperty.SetValue( this, property.GetValue(result, null), null );" – Chandra Mohan Jun 08 '14 at 12:04
  • try to cast like this: TargetProperty.SetValue( this, (TresultType)property.GetValue(result, null), null ); – TotPeRo Jun 08 '14 at 12:04
  • TargetProperty type is showing as System.Reflection.RuntimePropertyInfo. So i hope above solution will not solve my problem. Because TresultType already boolean – Chandra Mohan Jun 08 '14 at 12:07
  • “why the dynamic class properties are showing as System.Reflection.RuntimePropertyInfo” — not just dynamic. Properties of all types are shown as RuntimePropertyInfo. RuntimePropertyInfo inherits from PropertyInfo, which is documented in MSDN. – Soonts Jun 08 '14 at 12:12
  • So how can i make it work. How can i assign values to runtime propeties – Chandra Mohan Jun 08 '14 at 12:15
  • I don't know why down votes given. But i did research on how to map existing class properties to dynamic class properties. My problem is i cannot convert the existing class property type(here Boolean) to Target property type which is showing as System.Reflection.RuntimePropertyInfo – Chandra Mohan Jun 08 '14 at 12:29
  • Can you show us the rest of the code, where the dynamic object is created?? Your useage here is confusing. I get what you are trying to do in general, but some context would help to determine where the error lies. It looks like you might be setting up the dynamic object incorrectly. – XIVSolutions Jun 08 '14 at 12:38
  • We need to see how the properties themselves are being added to `this` when you create the dynamic object. – XIVSolutions Jun 08 '14 at 12:45
  • @XIVSolutions I have edited my question and added the function – Chandra Mohan Jun 08 '14 at 12:50
  • 1
    Not sure why the downvotes either. Upvoted to counter some of the bad juju. SO is getting a little off the chain with that recently. This was not a noob question which had been asked a gazillion times. You could have provided more information initially, but the question itself seemed reasonable to me. There are a lot of "experts" here anymore who seem to feel that if a question is not worthy of consideration for a doctoral dissertation, it should be down-voted with no explanation. – XIVSolutions Jun 08 '14 at 13:02

2 Answers2

2

If PropertyInfo.PropertyType == typeof(System.Reflection.RuntimePropertyInfo) then your property has type System.Reflection.RuntimePropertyInfo.

You say that you have created this type dynamically (using reflection emit, probably). There is probably a bug in that code.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 1
    Thank you so much.While passing propery type i used the following line property.getType() instead of "property.PropertyType". This solved the problem – Chandra Mohan Jun 08 '14 at 12:58
  • The following code in the above funtion DynamicType.CreateProperty(tb, property.Name, property.GetType()); should be rewritten as DynamicType.CreateProperty(tb, property.Name,property.PropertyType); – Chandra Mohan Jun 08 '14 at 13:00
1

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.

XIVSolutions
  • 4,442
  • 19
  • 24