如何找到我的 propertyinfo 类型?

发布于 2024-12-09 20:11:53 字数 723 浏览 4 评论 0原文

我有以下代码:

 public class EntityBase
{


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

 }

我的示例中的任何类都继承自 EntityBase;像这样:

  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; }
   }

我希望当我调用 person.Freez() 时,如果 person 具有一个类属性,例如 PersonAsset.the PersonAsset Freez() 方法引发; 我想我必须在 EntityBase Freez() 方法中使用反射。但是当我获得 PersonAsset 属性时 通过反射我怎样才能提高它的 Freez() 方法?或者我怎样才能找到我的 propertyinfo 是一个类?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈场末日恋爱 2024-12-16 20:11:53
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();
                    }
                }
            }
        }
    }
}
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();
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文