检索数据C#Windows窗体的通用函数 [英] Generic function for retrieving data C# windows forms
问题描述
我正在C#2008中开发Windows窗体应用程序.
我的人的内存中有一个数据表.
sex
,firstname
,lastname
,partner name
等字段,要使用的名称等.
我想创建一封电子邮件:
I''m working on a windows forms application in C# 2008.
I have a datatable in memory with persons.
Fields like sex
, firstname
, lastname
, partner name
,nameto use etc.
I want to create an email:
Dear Mr Johnston,
We would like to check our data:
Your first name: Leo
等
性别存储为1和0,所以我创建了一个函数:
Etc.
The sex is stored as 1 and 0, so I created a function:
if(sex = 0, "Mr", "Mrs")
但这变得非常复杂,必须为每个邮件创建一个.
所以我使用占位符,例如:
But this becomes very complicated and has to be created for every mail.
So I use placeholders like:
Dear %sexU% %LastnameUse%
%sexU% means sex written out with first capital letter.
%LastnameUse% means the last name the person wants to use (eg married women)
缺点是我必须检查代码以知道可以使用哪些占位符,因此我尝试制作一个占位符表:
The disadvantage is that I have to check the code to know what placeholders can be used, so I tried to make a table of placeholders:
SexU iif(datarow[Sex] == 1, "Mr", "Mrs")
LastnameUse iif(datarow[partner name] != "", Partner name, Last name)
问题是:如何将此表中包含的字符串作为函数执行?
我已经尝试过泛型委托,但是我无法使其正常工作.
我不知道要在哪里搜索,因此只需要正确方向的指针即可.
谢谢
罗布
可以说我想做一个函数:
The problem is: how can I execute the string contained in this table as a function?
I''ve tried generics delegates, but I couldn''t get it to work.
I don''t know where to search so a pointer in the correct direction could be all that is necessary!
Thanks
Rob
Lets say I want to make a function:
///FunctionDescription can be: "iif(sex==1, "Mr", "Mrs");
///FunctionDescription can be: "iif(datarow[partner name] != "", Partner name, Last name)"
///sex, partnername, lastname are all fields in datatable person
private string ReplaceFields(datatable person, string functiondescription)
{
return execute(function);
}
推荐答案
您可以为 SexU 和 LastNameUse 这样的keyValue对再创建一个表.
将有三列
1)您要寻找的字段 ex:SexU
2)您需要搜索 ex的键值:1,0
3)您需要替换 ex的相应值:Mr,Mrs
通过这种方式,您可以创建一个通用函数.
希望这些信息对您有帮助,
谢谢
-amit.
Hi,
you can create one more table for keyValue pair for such SexU and LastNameUse.
there will be three column
1)which field you are looking for ex:SexU
2)key value you need to search for ex : 1,0
3)corresponding value that you need to replace ex: Mr,Mrs
from this way you may create one generic function.
hope this information helpful to you,
thanks
-amit.
我的问题不是很清楚,因为我不知道自己在寻找什么,但反思就是答案!
My question wasn''t very clear because I didn''t know what I was looking for, but reflection was the answer!
private void button1_Click(object sender, EventArgs e)
{
DataTable dtPerson = new DataTable("Person");
dtPerson.Columns.Add(new DataColumn("LastName", typeof(string)));
dtPerson.Columns.Add(new DataColumn("FirstName", typeof(string)));
dtPerson.Columns.Add(new DataColumn("PartnerName", typeof(string)));
dtPerson.Rows.Add(new object[] { "Johnson", "John", "" });
dtPerson.Rows.Add(new object[] { "Rosevelt", "Mary", "Johnson" });
dtPerson.Rows.Add(new object[] { "Clinton", "Bill", "" });
string[] Fields = { "LastName", "FirstName", "PartnerName", "UseName" };
DelegateDemo myClass = new DelegateDemo();
for (int j = 0; j < dtPerson.Rows.Count; j++)
{
DataRow drPerson = dtPerson.Rows[j];
object[] mParam = new object[] { drPerson };
for (int i = 0; i <= Fields.GetUpperBound(0); i++)
{
System.Reflection.MethodInfo myMethodInfo = myClass.GetType().GetMethod(Fields[i]);
MessageBox.Show(myMethodInfo.Invoke(myClass, mParam).ToString());
}
}
}
}
class DelegateDemo
{
public virtual string LastName(DataRow drPerson)
{
return drPerson["LastName"].ToString();
}
public virtual string FirstName(DataRow drPerson)
{
return drPerson["FirstName"].ToString();
}
public virtual string PartnerName(DataRow drPerson)
{
return drPerson["PartnerName"].ToString();
}
public virtual string UseName(DataRow drPerson)
{
if (drPerson["PartnerName"].ToString() == "")
{
return drPerson["LastName"].ToString();
}
else
{
return drPerson["PartnerName"].ToString();
}
}
}
感谢您为我提供的帮助!
罗布
Thanks for your efforts to help me!
Rob
这篇关于检索数据C#Windows窗体的通用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!