简体   繁体   English

如何使用摘要从当前类中获取属性?

[英]How do I use snippets to get properties from the current class?

Is it possible for me to create a snippet and have it analyze a current class, get the properties of said class, and then create a sql function that writes out line by line each property in a command parameter. 我是否可以创建一个代码片段并分析当前的类,获取所述类的属性,然后创建一个sql函数,以逐行写出命令参数中的每个属性。

What I am looking for is doing something like this: 我正在寻找的是做这样的事情:

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;
    }

Basically I want the snippet to populate the entire Add function and fill in the @PropertyName and the Message.PropertyName in the command.Parameters.AddWithValue 基本上,我希望代码片段填充整个Add函数并在command.Parameters.AddWithValue填充@PropertyNameMessage.PropertyName

I don't think code snippets are powerful enough. 我认为代码片段不够强大。 Maybe the ReSharper's code templates are powerful enough but I don't think so, too. ReSharper的代码模板也许功能足够强大,但我也不这么认为。 You could look into using T4 templates if you really need or want code generation. 如果您确实需要或想要生成代码,则可以考虑使用T4模板

Personally I would suggest to avoid compiletime code generation altogether. 我个人建议完全避免编译时代码生成。 You could use reflection - easy but slow - or runtime code generation - complex but fast. 您可以使用反射-简单但缓慢-或运行时代码生成-复杂但快速。 If performance is not a primary concern I suggest to use reflection. 如果性能不是主要考虑因素,则建议使用反射。

 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;
    }
}

Note that you have to introduce and implement the interface IMessageWithIMID in order to access the property IMID . 请注意,您必须引入并实现接口IMessageWithIMID才能访问属性IMID

internal interface IMessageWithIMID
{
    Int32 IMID { get; set; }
}

Note that you also don't need a data read - you can just use ExecuteScalar() . 请注意,您也不需要读取数据-您可以使用ExecuteScalar() This turns 这变成

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

into 进入

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

and you are done. 到此为止。

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

相关问题 如何从 static class 上的 static 属性中获取值? - How do I get the values from static properties on a static class? 当我有类但子类名称为字符串时,如何从子类获取属性 - How do I get the properties from a subclass when I have the class but subclass name as a string 如何遍历我的类属性并获取其类型? - How do I loop through my class properties and get their types? 如何使用 .Include 方法从数据库中获取多个相关属性(不同类型)? - How do I use the .Include method to get multiple related properties (of different types) from database? 如何使用反射来获取显式实现接口的属性? - How do I use reflection to get properties explicitly implementing an interface? Silverlight:如何根据当前设置类动态更新属性? - Silverlight: How do I update properties dynamically in accordance with a current settings class? 如何设计包含查找表属性的类? - How do I design a class containing properties from lookup tables? 如何从父 class 中的名称动态设置属性? - How do I Dynamically set properties from the name in a parent class? 如何从另一个 class 引用控件的属性? - How do I reference the properties of a control from another class? 如何从 OkObjectResult 获取动态属性 - How do I get dynamic properties from OkObjectResult
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM