开发者

How can i find my propertyinfo type?

I have the following code:

 public class EntityBase
{


 public virtual void Freez(EntityBase obj)
 {
    //TO DO

 }

Any Class in my sample inherit from EntityBase; like this:

  public class Person:EntityBase
    {
       public Person()
       {
           this.PersonAsset = new Asset { Title = "asset1" }开发者_运维百科;
       }
        public string Name { get; set; }
       public Asset PersonAsset{get;set;}


    }
   public class Asset : EntityBase
   {
       public string Title { get; set; }
   }

I want when i invoke person.Freez() if person has a property that is a class such as PersonAsset.the PersonAsset Freez() method raised; I think i must using reflection in EntityBase Freez() method.But when i get PersonAsset property by reflection how can i raise its Freez() method?Or How can i find my propertyinfo is a Class?


public virtual void Freez()
{
    foreach (var prop in this.GetType().GetProperties())
    {
        if (prop.PropertyType.IsClass && typeof(EntityBase).IsAssignableFrom(prop.PropertyType))
        {
            var value = (EntityBase) prop.GetValue(this, null);
            value.Freez();
        }

        if (typeof(ICollection).IsAssignableFrom(prop.PropertyType))
        {
            var collection = (ICollection)prop.GetValue(this, null);
            if (collection != null)
            {
                foreach (var obj in collection)
                {
                    if (obj is EntityBase)
                    {
                        ((EntityBase)obj).Freez();
                    }
                }
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜