我们可以动态获取属性名称吗 [英] Can We Get The Name Of Properties Dynamically

查看:56
本文介绍了我们可以动态获取属性名称吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了具有不同属性的不同数量的类,并且我制作了一个仅检查该特定类中的属性值是否为null的函数,所以我只想知道我可以通过任何方式该类的引用和该类的属性名称是动态生成的,因为每个类具有不同名称的不同属性... ???

i have made different number of classes with different properties and i have made a function which simply checks either the property value in that particular class is coming null or not so i just wanted to know that is there any way that i just pass the references of that class and the property name of that class comes dynamically since every class have different properties with different name...???

推荐答案

Reflection是您所需要的需要这样做.几乎肯定不值得.
Reflection is what you need to do this. It''s almost certainly not worth it.


此代码将帮助您实现克里斯蒂安·格劳斯所说的话.

this code will help you how to implement what Christian Graus said.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PropertyInfo[] pinfo = typeof(MYClass).GetProperties();
    }
}
public class MYClass
{
    public MYClass()
    { }
    public int MyProperty1 { get; set; }
    public int MyProperty2 { get; set; }
    public int MyProperty3 { get; set; }
}


reflection是执行此操作的最合适方法,请尝试...
reflection is the most appropriate way to do this , try it ...


这篇关于我们可以动态获取属性名称吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆