[英]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
填充@PropertyName
和Message.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.