我有一部分代码在运行时接受lambda表达式,然后我可以编译和调用它.
Something thing;
Expression<Action<Something>> expression = (c => c.DoWork());
Delegate del = expression.Compile();
del.DynamicInvoke(thing);
Run Code Online (Sandbox Code Playgroud)
为了节省执行时间,我将这些已编译的委托存储在缓存中,Dictionary<String, Delegate>其中键是lambda表达式字符串.
cache.Add("(Something)c => c.DoWork()", del);
Run Code Online (Sandbox Code Playgroud)
对于完全相同的调用,它工作正常.但是我意识到我可以收到相当的lambda,例如"d => d.DoWork()",我实际上应该使用相同的委托,而我不是.
这让我想知道是否有一种干净的方式(读取"不使用String.Replace",我已经将其作为临时修复)来替换lambda表达式中的元素,比如可能替换它们arg0以便两者都可以
(c => c.DoWork()) 和 (d => d.DoWork())
(arg0 => arg0.DoWork())通过使用类似于在lambda中注入Expression.Parameter(Type,Name)的东西进行转换和比较.
那可能吗 ?(答案可以包括C#4.0)
我似乎无法XDocument.Descendants ()在文档中找到任何其他内容.有没有办法做到这一点?
我有一个像这样的字符串:
"20090212"
Run Code Online (Sandbox Code Playgroud)
我想转换为有效的C#datetime.
我是否需要解析它,因为这看起来太多了?
以下是与Autofac配合使用的AspComet项目的代码摘录.
public MessageBus(IClientRepository clientRepository, Func<IMessagesProcessor> messagesProcessorFactoryMethod)
{
this.clientRepository = clientRepository;
this.messagesProcessorFactoryMethod = messagesProcessorFactoryMethod;
}
Run Code Online (Sandbox Code Playgroud)
如何Func<IMessagesProcessor> messagesProcessorFactoryMethod用温莎注入" ",是否可能?
谢谢.
我正在寻找一种方法来对linq中的子集合进行IN子句查询.
我有一个例子如下:
Entity: Product.CategoryAssignments - this is an IList<Category> that the product is assigned to. Product can be assigned to multiple categories.
Run Code Online (Sandbox Code Playgroud)
我想检索在类别列表中匹配的所有产品,即
IList<Category> selectedCategories = GetUsersSelectedCategories();
Run Code Online (Sandbox Code Playgroud)
//怎么做的事情
IList<Products> products = from p in repository where p.CategoryAssignments.Contains(?) select p;
Run Code Online (Sandbox Code Playgroud)
任何提示赞赏?
c# ×3
delegates ×2
linq ×2
datetime ×1
expression ×1
lambda ×1
linq-to-xml ×1
xelement ×1
xml ×1