简体   繁体   English

使用system.reflection列出一个类文件

[英]using system.reflection to list a class fileds

i need to get a list to store all fields - values in a class 我需要获取一个列表来存储所有字段-类中的值

the class is just a few of public const string variables as i pasted below. 该类只是我下面粘贴的一些公共const string变量。

public class HTDB_Cols
{
    public class TblCustomers
    {
        public const string CustID  = "custID",
               Name  = "name",
         CustType  = "custType",
         AddDate  = "addDate",
         Address  = "address",
         City  = "city",
         Phone  = "phone",
         Cell  = "cell";
    }
}

this is a method that returns a list of strings that enables me to have a list of strings representing all my tables columns names , though somthing is not working with this code as i get an error 这是一种返回字符串列表的方法,该方法使我能够使用代表我所有表列名称的字符串列表,但是由于出现错误,某些东西无法与此代码一起使用

" Non-static field requires a target ". 非静态字段需要目标 ”。

public class GetClassFields
{

        public static List<string> AsList(string TableName)
        {


                    return typeof(HTDB_Cols).GetNestedTypes()
                    .First(t => String.Compare(t.Name, TableName, true) == 0)
                    .GetFields()
                    .Select(f => f.GetValue(null) as string)
                    .ToList();

        }
}

trying to use it as follows : 尝试如下使用它:

foreach (string tblCol in RobCS_212a.Utils.Reflct.GetClassFields.AsList      (DBSchema.HTDB_Tables.TblCustomers))
{
    Response.Write(string.Concat(tblCol, "<br />"));
}

Field 'tbName' defined on type 'DBSchema.HTDB_Cols+TblTimeCPAReport' is not a field on the target object which is of type 'DBSchema.HTDB_Cols'. 在类型“ DBSchema.HTDB_Cols + TblTimeCPAReport”上定义的字段“ tbName”不是目标对象在“ DBSchema.HTDB_Cols”类型上的字段。

Your code was close. 您的代码已关闭。 There were two issues, both located in the arguments to your linq select method call: 有两个问题,都位于linq select方法调用的参数中:

  • Your class HTDB_Cols is a non-static class and the string values you are trying to retrieve are instance members. 您的HTDB_Cols类是一个非静态类,您尝试检索的字符串值是实例成员。 Thus when you are trying to pull instance members out of a class, you have to pass an instance of the class to the FieldInof.GetValue method. 因此,当您尝试将实例成员从类中拉出时,必须将类的实例传递给FieldInof.GetValue方法。 In my code below I create an instance of your class in the variable "instanceOfClass". 在下面的代码中,我在变量“ instanceOfClass”中创建您的类的实例。 You can see this in the documentation for the FieldInfo class 您可以在FieldInfo类的文档中看到这一点

  • The value returned from FieldInfo.GetValue is an object. 从FieldInfo.GetValue返回的值是一个对象。 You have to explicitly cast it to a string using the ToString method or the (string) cast. 您必须使用ToString方法或(string)强制转换将其显式转换为字符串。

With these two changes your method works. 通过这两个更改,您的方法就可以使用。 A listing is as follows: 清单如下:

public class GetClassFields
{
    public static List<string> AsList(string tbl)
    {
        var instanceOfClass = new HTDB_Cols();
        return typeof(HTDB_Cols).GetNestedTypes()
                                .First(t => String.Compare(t.Name, tbl, true) == 0)
                                .GetFields()
                                .Select(f => f.GetValue(instanceOfClass).ToString())
                                .ToList<String>();
    }
}

You can call this function as follows: 您可以按以下方式调用此函数:

var fields = GetClassFields.AsList("TblCustomers");

which returns the desired information: 返回所需的信息:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM