我有一个 C# 方法,我想用它来更新一些数据。该方法可以传递一个字符串、一个双精度数、一个整数
public void Update(string ac, string pr, string fld, Int32 intVal = null, double dblVal = null, string strVal = null)
{
根据 fld 的值,我想更新表中的某个字段名称。
有没有什么方法可以对函数参数进行编码,这样我就不必提供一长串可能的值类型?
理想情况下,我希望能够像这样调用这个函数:
Update("key1", "key2", "location", "London");
Update("key1" , "key2", "beds", 2);
Update("key1" , "key2", "price", 123.45);
然后在函数更新中我想要:
public void Update(string ac, string pr, xxx, yyy)
{
try
{
vm.Product = _product.Get(ac, pr);
vm.Product.xxx = yyy;
_product.AddOrUpdate(vm.Product);
}
catch (Exception e) { log(e); }
}
在 xxx 中,我想给出字段名称和 yyy 实际值。也许在函数内部我可以使用基于字段名称的 case 方法进行某种类型的转换。我只有少量字段名称,因此这很容易。另外,也许有一种方法可以从 vm 类的反射中找到数据类型,但这远远超出了我的知识范围。