c# – 如何将void返回扩展方法传递给动态返回扩展方法?
作者:互联网
我想传递一个扩展方法,该方法将void作为参数返回给另一个返回dynamic的扩展方法.
public static void AddTo(this Object entity, Object parent)
{
parent.GetCollectionOf(entity).Add(entity);
}
public static dynamic And(this Object entity, Action method)
{
entity.method(parent);
return entity;
}
我想用它这样的东西,
dynamic parent = MakeNew(parentType);
dynamic entity = MakeNew(type).And(AddTo(parent));
我喜欢将任何void方法传递给And()但仍返回它扩展的对象.我希望动态返回类型不成问题.
这种事情的语法是什么?
解决方法:
你能这样做吗?
myObjects
.Where(d => d.true == true && d.Value == 77)
.Update(e => { e.Value = 1; e.true = false; } );
仔细使用我的linq,它可能随时爆炸;-)
/// <summary>
/// Used to modify properties of an object returned from a LINQ query
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <param name="input">The source</param>
/// <param name="updater">The action to perform.</param>
public static TSource Update<TSource>(this TSource input, Action<TSource> updater)
{
if (!updater.IsNull() && !input.IsNull())
{
updater(input);
}
return input;
}
要完全解释这个:
public DataRow DoSomething(DataRow dataRow)
{
//DoSomething
return dataRow;
}
var query = from dataRow in myDataTable.Rows.Cast<DataRow>()
where
Double.TryParse(dataRow["Distance"].ToString(), out distance)
&& distance > (11) && distance <= 99
select dataRow.Update(f => DoSomething(f));
标签:c,delegates,extension-methods,action,func 来源: https://codeday.me/bug/20190630/1337360.html