Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何循环传递给方法的类并检索它';C中的s属性值#_C#_Reflection - Fatal编程技术网

C# 如何循环传递给方法的类并检索它';C中的s属性值#

C# 如何循环传递给方法的类并检索它';C中的s属性值#,c#,reflection,C#,Reflection,首先,如果这个问题对网站上的其他信息来说是多余的,请原谅我。我已经浏览了其他关于反思的帖子,但就我所做的而言,似乎没有任何意义 其思想是将一个方法传递给一个类,然后遍历该对象的属性,并将其值赋给一个返回值。返回值稍后将用于动态创建SQL语句。我已经使用PropertyInfo创建了循环,并且能够根据switch语句判断属性的类型。问题是,一旦我遇到了属性的特定情况,我就很难找出如何将特定属性值分配给稍后返回的列表。这是我的代码,提前谢谢你的帮助 public List<string

首先,如果这个问题对网站上的其他信息来说是多余的,请原谅我。我已经浏览了其他关于反思的帖子,但就我所做的而言,似乎没有任何意义

其思想是将一个方法传递给一个类,然后遍历该对象的属性,并将其值赋给一个返回值。返回值稍后将用于动态创建SQL语句。我已经使用PropertyInfo创建了循环,并且能够根据switch语句判断属性的类型。问题是,一旦我遇到了属性的特定情况,我就很难找出如何将特定属性值分配给稍后返回的列表。这是我的代码,提前谢谢你的帮助

    public List<string> BuildParametersFromObject(clsBook oBook, clsStudent oStud)
    {
        List<string> lstRetVal = new List<string>();
        string sTemp = "";
        string sPropType = "";
        int ii;
        if (oBook == null)
        {
            PropertyInfo[] ObjProperties = typeof(clsStudent).GetProperties();
            foreach (PropertyInfo StudProp in ObjProperties)
            {
                ii = 0;
                sPropType = Convert.ToString(StudProp.PropertyType);
                switch (sPropType)
                {
                    case "System.String":
                        lstRetVal.Add(StudProp.GetValue(clsStudent, null));
                        break;
                    case "System.Int32":

                        break;
                    case "System.Decimal":

                        break;
                    case "System.Boolean":

                        break;
                    case "System.DateTime":

                        break;
                }

            }
        }
        return lstRetVal;
    }
public List buildParameters from对象(clsBook oBook、clsStudent oStud)
{
List lstreval=新列表();
字符串sTemp=“”;
字符串sPropType=“”;
int ii;
如果(oBook==null)
{
PropertyInfo[]ObjProperties=typeof(clsStudent.GetProperties();
foreach(对象属性中的属性信息StudProp)
{
ii=0;
sPropType=Convert.ToString(StudProp.PropertyType);
开关(sPropType)
{
案例“System.String”:
添加(StudProp.GetValue(clsStudent,null));
打破
案例“System.Int32”:
打破
案例“System.Decimal”:
打破
案例“System.Boolean”:
打破
案例“System.DateTime”:
打破
}
}
}
返回lstreval;
}

在其他每种情况下,只需对GetValue()的结果调用ToString()


显然,如果您需要特定的格式,您可以在添加到列表之前执行此操作。

您的代码不会编译,因为

StudProp.GetValue(clsStudent, null)
要求它的第一个参数是一个对象,而你给这个方法的肯定是一个类符号

其次,我试图了解您打算如何使用两个参数:

  • clsBook-oBook
  • clsStudent-oStud
从您在那里写的内容中,我了解到,如果
oBook
不为空,那么您可以将结果添加到返回列表中。我现在就不谈这个了

第二个最大的问题是您是否需要
oStud
参数

假设您解决了编译问题,并且您的代码进行了编译。 如果您尝试完全删除该参数,即

public List<string> BuildParametersFromObject(clsBook oBook, clsStudent oStud)
解释

我所做的是将您的显式筛选(使用开关)修改为一个基本数组(您希望在添加结果时支持的类型列表),并由此删除了不必要且完全难以维护的“按名称比较”类型。比较类型本身要干净得多。世界上可能有RunninThruLife,但其中只有一个是你


最大的错误是,不是从clsStudent实例获取属性值,而是从clsStudent本身(这是一个类)获取属性值。

这似乎是个坏主意。如果您试图将所有内容转换为字符串,我感觉这些代码可能会导致打开sql注入漏洞。我转换这些值,因为我需要它们不同(例如,十进制表示sql server类型货币,int表示int,等等),我总是对SQL语句使用参数方法,而不是字符串连接。。。我的方法是应用服务器数据类型和子字符串的第一个字母来决定应该添加哪种类型,例如在insert。。。“i_@field1、v_@field2、b_@field3等等,这看起来像是性能消耗:例如,您有一个原本是十进制的值,您将其转换为字符串,然后必须将字符串解析回十进制,然后再将其发送到db。您不会注意到这一点,但对于一个不知道是否将使用它返回的数据层,您不会注意到这一点在一个查询中有上千条记录,或者在一个web服务器上运行数千个会话的较小查询,这是你想要避免的性能税。谢谢你,为了让它工作,我不得不添加实例变量来代替类名,但这对我来说是一个正确的方向。再一次,这是成功的。都德:clsStudent是一个class symbol。这无法编译。您尝试过吗?这就像尝试将类型字符串与字母“a”typeof(clsStudent)和StudProp.GetValue(clsStudent,…)连接起来一样这两种情况都意味着你没有仔细阅读问题,提问者没有理解你的答案+1我完全同意,我很抱歉没有注意到这是一个类符号而不是一个对象。我试图指出,要将其他类型放入字符串列表中,你可以调用它们的ToString函数。我也同意正如你在回答中指出的,女巫是不必要的。
public List<string> BuildParametersFromObject(clsBook oBook, clsStudent oStud)
public List<string> BuildParametersFromObject(clsBook oBook)
// prepare your little filter
private static readonly Type[] allowedTypes = new Type[] { 
    typeof(bool), typeof(string), typeof(int), 
    typeof(decimal), typeof(DateTime)
};

public List<string> BuildParametersFromObject(clsBook oBook, clsStudent oStud)
{
    List<string> lstRetVal = new List<string>();
    string sTemp = "";
    string sPropType = "";
    int ii;
    if (oBook == null)
    {

        PropertyInfo[] ObjProperties = typeof(clsStudent).GetProperties();
        foreach (PropertyInfo StudProp in ObjProperties)
        {
            // if the property's type is not one of the 5 magnificence
            // simply jump to the next cycle of the foreach loop
            if (!allowedTypes.Contains(StudProp.PropertyType)) 
                continue;

            object pureValue = StudProp.GetValue(oStud);
            // what you were using is not ok: StudProp.GetValue(clsStudent)
            // because you're trying to retrieve the value of a property
            // defined by the clsStudent class
            // and in the process you also need to point out an instance
            // of that particular class, but instead of doing that
            // you were pointing out an instance of Type i.e. clsStudent itself

            string stringValue = pureValue + "";
            // simply concatenate the pureValue with a blank string
            // (allowing imminent nulls to be displayed as empty strings instead of crashing your thread with NullReferenceException)

            lstRetVal.Add(stringValue);
            // just add your stringified value to the list

        }


    }
    return lstRetVal;
}