首页 > 其他 > 详细

通过反射,获取类的属性

时间:2016-06-27 19:10:30      阅读:239      评论:0      收藏:0      [点我收藏+]

http://stackoverflow.com/questions/3723934/using-propertyinfo-to-find-out-the-property-type

https://www.codewars.com/kata/56c22c5ae8b139416c00175d/train/csharp

 

using StriveObjects;
using System.Reflection;
using System;

public class Strive
{
   public static string Match(Candidate c, Job j)
        {
             string str = string.Empty;
            var list = c.GetType().GetProperties();
            foreach (PropertyInfo item in list)
            {
                Console.WriteLine($"{item}  {item.PropertyType}");
            }

            list = j.GetType().GetProperties();
            foreach (PropertyInfo item in list)
            {
                Console.WriteLine($"{item}  {item.PropertyType}");
            }
            return str;
        }
}

 

 

using System;

public class Strive
{
    public static bool Match(Candidate c, Job j)
    {
        var pro1 = c.GetType().GetProperty("MinSalary");
        if (pro1 == null)
        {
            throw new ArgumentException(nameof(c));
        }
        int min = Convert.ToInt32(pro1.GetValue(c, null));

        var pro2 = j.GetType().GetProperty("MaxSalary");
        if (pro2 == null)
        {
            throw new ArgumentException(nameof(j));
        }
        int max = Convert.ToInt32(pro2.GetValue(j, null));

        if (min == 0 || max == 0)
        {
            throw new ArgumentException();
        }

        return min * 0.9 <= max;
    }
}

 

通过反射,获取类的属性

原文:http://www.cnblogs.com/chucklu/p/5620936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!