创建动态lambda方法委托 [英] Creating a dynamic lambda method delegate
问题描述
说我们有一个像这样的课程:
Say we have a class like this :
public class someclass
{
public string Name;
public string Address;
public int Code;
}
有没有办法创建这样的方法
Is there a way to create a method like this
public delegate Insert(string Name, string Address, int Code);
推荐答案
我认为VirtualBlackFox
的答案将在本页帮助您:
http://stackoverflow.com/a/1575153 [ http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx [ ^ ]
希望对您有所帮助.
I think the answer ofVirtualBlackFox
will help you at this page :
http://stackoverflow.com/a/1575153[^]
CodeDOM documentation :
http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx[^]
Hope it helps.
您好,
我认为您可以使用操作 [功能 [
Hi,
I think you can use Action [^] and Func[^] predefined delegates.
Using them looks like this:
Func<string,> insert = (Name, Address, Code) =>
{
// Do something with Name, Address and Code
return String.Format("Data inserted:[Name={0}; Address={1}; Code={2}]", Name, Address, Code);
};
string result = insert("Martin Arapovic", "Split, Croatia", 100);
Console.WriteLine(result);
实际上,这可以在设计时完成,但是要在运行时使用它,我想您将使用ExpressionTrees:
1. http://www.thejoyofcode.com/Lambda_and_Expression_Trees.aspx [ http://msdn.microsoft.com/en-us/library/bb397951.aspx [ ^ ]
3. http://www.abhisheksur.com/2010/09/use-of-expression-trees-in-lamda-c.html [ http://blogs.msdn.com/b/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx [ ^ ]
Actually this can be done in design time, but to use it in runtime I think you will work with ExpressionTrees:
1. http://www.thejoyofcode.com/Lambda_and_Expression_Trees.aspx[^]
2. http://msdn.microsoft.com/en-us/library/bb397951.aspx[^]
3.http://www.abhisheksur.com/2010/09/use-of-expression-trees-in-lamda-c.html[^]
3. http://blogs.msdn.com/b/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx[^]
这篇关于创建动态lambda方法委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!