Skip to main content
 首页 » 编程设计

c#之如何为此创建扩展方法/lambda 函数

2024年02月23日20zhwl

我正在做以下事情:

其中“fc”是一个带有属性列表的控件。 “nc”是我放置属性值的位置。

根据下面看到的路线,我将不得不这样做 10 次以收集/映射 10 个属性。有没有办法减少重复?

   FormControl fc; 
   FormControlProperty fp; 
   NoteTemplateControl nc; 
   fp = fc.Property.Find(i => i.name == "Display"); 
   if (fp != null) 
   { 
       nc.Display = fp.Value; 
   } 
   fp = fc.Property.Find(i => i.name == "Text"); 
   if (fp != null) 
   { 
       nc.Text = fp.Value; 
   } 

谢谢。

请您参考如下方法:

public static void Map(this Control fc, string name, Action<string> assign) 
{ 
   var fp = fc.Property.Find(i => i.name == name); 
   if (fp != null) assign(fp.Value); 
} 

用法:

theControl.Map("Display", v => theControl.Display = v);