dimanche 5 juin 2016

How to associate a specific property to a business rule using reflections?

My task is to get all the properties from a class, find a specific one and associate that property in CSLA business rule as a primary property. However, I don't know how to associate it

[Serializable]
public partial class MyClass: BusinessBase<MyClass>
{     
    private static readonly ValuePropertyInfo<int> MyClassIdProperty = 
        RegisterValueProperty<int>(x => x.MyClassIdProperty);
    private static readonly StringPropertyInfo MyClassCodeProperty =
        RegisterStringProperty(
           x => x.MyClassCode, 
           new StringPropertyInfoOptions { IsRequired = true, MaxLength = 32 });

And now in my another class, I am only interested in MyClassCodeProperty which I've extracted using reflections like this:

PropertyInfo[] propInfos;
propInfos = typeof(MyClass).GetProperties(BindingFlags.Public | BindingFlags.Static);
Array.Sort(propInfos, delegate (PropertyInfo propInfo1, PropertyInfo propInfo2) 
  { return propInfo1.Name.CompareTo(propInfo2.Name); });

foreach (PropertyInfo propertyinfo in propInfos)
{
    if(propertyinfo.Name == "MyClassCodeProperty")
    {              
        BusinessRules.AddRule(new MyRule{ 
           PrimaryProperty =  //? I don't know what goes here 
        });
    }
}





Aucun commentaire:

Enregistrer un commentaire