变量传递给asp.net中的'typeof'运算符 [英] variable to pass to 'typeof' operator in asp.net

查看:133
本文介绍了变量传递给asp.net中的'typeof'运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在belwo代码中,我想归纳"typeof",这就是为什么我设计将参数"typ"传递给"typeof"运算符的原因.我没有典型"变量的想法,但它不起作用!所以,我如何概括将要传递给``typeof''运算符的变量,它是什么变量类型!我的代码:

In the belwo code i want to generalize ''typeof'' that''s why i have designed to pass parameter ''typ'' to ''typeof'' operator . I don''t have the idea of ''typ'' variable but it''s not working !!! So, how i can generalized the variable to be passed to ''typeof'' operator and what is it''s variable type !!!! My code :

public static string BuildWhereCondition(Type typ, string columnName, string columnValue)
{
    string whereCondition = string.Empty;
    PropertyInfo columnInfo = typeof(typ).GetProperty(columnName);
    if (columnInfo != null)
    {
       if (columnInfo.PropertyType == typeof(int))
       {
           whereCondition = columnName + "=" + columnValue;
       }
       else if (columnInfo.PropertyType == typeof(String))
       {
           whereCondition = columnName + " LIKE '%" + columnValue + "%'";
       }
       else if (columnInfo.PropertyType == typeof(DateTime))
       {
           whereCondition = columnName + "='" + columnValue + "'";
       }
    }
    return whereCondition;
}


在此先谢谢您.


Thanks in advance .

推荐答案

typeof(typ)Type.试试这个:
The typeof(typ) is Type. Try this:
if(typ.Equals(System.Int)) {
   // ...
}



但是,串联sql命令字符串是软件的主要风险.最好将参数添加到SqlCommand.检查以下链接:
http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/AddparameterstotheSqlCommand.htm [ ^ ]

祝你好运!



However, concatenating sql command strings is a major risk in software. It would be better to add parameters to the SqlCommand. Check the link below:
http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/AddparameterstotheSqlCommand.htm[^]

Good luck!


阅读此

typeof(c#) [
read this

typeof(c#)[^]


这篇关于变量传递给asp.net中的'typeof'运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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