Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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#_Reflection_Code Snippets - Fatal编程技术网

C# 如何使用代码段从当前类获取属性?

C# 如何使用代码段从当前类获取属性?,c#,reflection,code-snippets,C#,Reflection,Code Snippets,我是否可以创建一个代码段,让它分析当前类,获取所述类的属性,然后创建一个sql函数,逐行写出命令参数中的每个属性 我想做的是这样的事情: public static int Add(MyObject Message) { MySqlConnection connection = new MySqlConnection(MySqlConnection); MySqlCommand command = new MySqlCommand("Add_Message", c

我是否可以创建一个代码段,让它分析当前类,获取所述类的属性,然后创建一个sql函数,逐行写出命令参数中的每个属性

我想做的是这样的事情:

public static int Add(MyObject Message) {
        MySqlConnection connection = new MySqlConnection(MySqlConnection);
        MySqlCommand command = new MySqlCommand("Add_Message", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@IMFromUserID", Message.IMFromUserID);
        command.Parameters.AddWithValue("@IMToUserID", Message.IMToUserID);
        command.Parameters.AddWithValue("@IMMessage", Message.IMMessage);
        command.Parameters.AddWithValue("@IMTimestamp", Message.IMTimestamp);
        connection.Open();
        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read()) {
            Message.IMID = (int)reader["IMID"];
        }
        command.Dispose();
        connection.Close();
        connection.Dispose();
        return Message.IMID;
    }
基本上,我希望代码段填充整个Add函数,并在command.Parameters.AddWithValue中填充@PropertyName和Message.PropertyName。我认为它们不够强大。也许这些功能足够强大,但我也不这么认为。如果您真的需要或想要生成代码,您可以考虑使用

我个人建议完全避免编译时代码生成。您可以使用反射(简单但缓慢)或运行时代码生成(复杂但快速)。如果性能不是主要问题,我建议使用反射

 public static Int32 Add<TMessage>(TMessage message)
     where TMessage: IMessageWithIMID
 {
    using (var connection = new MySqlConnection(connectionString))
    using (var command = new MySqlCommand("Add_Message", connection))
    {
        command.CommandType = CommandType.StoredProcedure;

        // We look only at public instance properties but you can easily
        // change this and even use a custom attribute to control which
        // properties to include.
        var properties = typeof(TObject).GetProperties(BindingFlags.Public |
                                                       BindingFlags.Instance);

        foreach (var property in properties)
        {
            var parameterName = "@" + property.Name;

            var value = property.GetValue(message, null);

            command.Parameters.AddWithValue(parameterName, value);
        }

        connection.Open();

        message.IMID = (Int32)command.ExecuteScalar();

        return message.IMID;
    }
}
请注意,您也不需要读取数据,只需使用。这变成

using (var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        message.IMID = (Int32)reader["IMID"];
    }
}
进入


你完成了。

奇怪的是,这对我来说确实很有效。我希望开始为应用程序构建一个基础框架,这将真正帮助我开始。基本上,我想为CRUD创建一个模板,但如果我使用泛型,那么这应该能满足我的需要。
using (var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        message.IMID = (Int32)reader["IMID"];
    }
}
message.IMID = (Int32)command.ExecuteScalar();