Easiest way to set the value of a ContentPart property knowing its string name

Topics: Customizing Orchard, Writing modules
Aug 16, 2013 at 5:03 PM
Edited Aug 16, 2013 at 5:07 PM
class MyPart : ContentPart<MyRecord>
{
string MyPropertyName {get;set;}
}

Unfortunately this does not work ?
myPart["PropertyName"] = value;

How to do this ?

I am testing things as this but it seems rather complicated for dynamic objects

object targetProp;           
GetMemberBinder binder = (GetMemberBinder)Microsoft.CSharp.RuntimeBinder.Binder.GetMember(0, "PropertyName", mytPart.GetType(), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(0, null) });
 ret = myPart.TryGetMember(binder, out targetProp);
if (ret)
      targetProp = strVal;
Coordinator
Aug 16, 2013 at 6:15 PM
Reflection.
Aug 16, 2013 at 6:29 PM
Edited Aug 16, 2013 at 6:32 PM
Absolute Zenitude Of the Answer, good :)
I am getting old and reflection is not as fast as in the past...
Something as this ?
        Type type       = targetPart.GetType() ;
        PropertyInfo propertyInfo = type.GetProperty( part.PropertyName, BindingFlags.Instance|BindingFlags.Public , null , typeof(String) , new Type[0] , null );
        if (propertyInfo == null)
            return false;
        propertyInfo.SetValue( targetPart , strVal , null ) ;
That's strange that c# does not provide something easier to have a uniform API for properties in dynamic objects.
Coordinator
Aug 16, 2013 at 6:58 PM
Yes, that sounds about right. I don't disagree about dynamic...