开发者

How can i find my propertyinfo type?

开发者 https://www.devze.com 2023-04-12 19:58 出处:网络
I have the following code: public class EntityBase { public virtual void Freez(EntityBase obj) { //TO DO

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

精彩评论

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

关注公众号