如何将 void 返回扩展方法传递给动态返回扩展方法?

发布于 2024-12-03 02:12:15 字数 523 浏览 7 评论 0原文

我想将一个返回 void 作为参数的扩展方法传递给另一个返回动态的扩展方法。

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() 但仍然返回它扩展的对象。我希望动态返回类型没有问题。

这种事情的语法是什么?

I want to pass an extension method that returns void as a parameter to another extension method that returns 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;
}

I'd like to use it something like this,

dynamic parent = MakeNew(parentType);    
dynamic entity = MakeNew(type).And(AddTo(parent));

I like to pass any void method into And() but still return the object it extended. I hope the dynamic return type is not problematic.

What is the syntax for this kind of thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白馒头 2024-12-10 02:12:15

我问你的问题正确吗?

dynamic entity = MakeNew(type).And(() => 
{
  AddTo(parent); 
}); 

Have I got your question right?

dynamic entity = MakeNew(type).And(() => 
{
  AddTo(parent); 
}); 
自此以后,行同陌路 2024-12-10 02:12:15

你可以这样做吗?

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));

Could you perhaps do it like this?

myObjects
        .Where(d => d.true == true && d.Value == 77)
        .Update(e => { e.Value = 1; e.true = false; } );

Use my linq carefully, it could explode at any moment ;-)

    /// <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;
    }

To explain this fully:

    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));
动次打次papapa 2024-12-10 02:12:15

我认为 C# 还不够“动态”,无法完成我认为您想要的事情。

And 方法将不起作用,因为 entity 参数的类型为 object,因此 entity.method(parent) 将不起作用。即使您将 entity 定义为 dynamic 类型,C# 也会尝试找到一个名为“method”的方法来调用。您可以在您的示例中执行此操作:

public static dynamic And(this Object entity, Action method, object parameter) 
{     
      method(entity, parameter);     
      return entity; 
}

dynamic entity = MakeNew(type).And(AddTo, parameter); 

I think that C# is not yet "dynamic" enough to do the thing I think you want.

The And method won't work, since the entity parameter is of type object, so entity.method(parent) will not work. Even if you define entity to be of type dynamic, C# will try to find a method called "method" to call. You can do this in your example:

public static dynamic And(this Object entity, Action method, object parameter) 
{     
      method(entity, parameter);     
      return entity; 
}

and

dynamic entity = MakeNew(type).And(AddTo, parameter); 
夏末 2024-12-10 02:12:15

我怀疑你实际上想要这个:

public static void AddTo(this Object entity, Object parent)
{
    parent.GetCollectionOf(entity).Add(entity);
}

public static dynamic And(this Object entity, Action<object> method)
{
    method(entity);
    return entity;
}

dynamic entity = MakeNew(type).And(entity => entity.AddTo(parent));

话虽如此,但仍然不清楚 parent 从哪里开始......

I suspect you actually want this:

public static void AddTo(this Object entity, Object parent)
{
    parent.GetCollectionOf(entity).Add(entity);
}

public static dynamic And(this Object entity, Action<object> method)
{
    method(entity);
    return entity;
}

dynamic entity = MakeNew(type).And(entity => entity.AddTo(parent));

Having said that, it's still not clear where parent is coming from to start with...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文