小编Rob*_*ing的帖子

有没有办法在所有构造函数运行后立即自动调用特定方法?

我希望能够在构造派生对象时自动调用特定方法,但是我想不出怎么做.以下代码说明.另一个答案是OnLoad,但我在Mac上为Unity做这个,OnLoad似乎不支持我的平台.有什么建议?

public class Parent {

    public Parent ()
    {
        // A. Stuff to do before child constructor code runs
        DoThisAutomaticallyAfterConstruction();
    }

    public void DoThisAutomaticallyAfterConstruction()
    {
        // C. In this example, this will run after A, before B. I want it to run ABC
    }
}

public class Child : Parent {

    public Child () : base()
    {
        // B. Stuff to do here after parent constructor code runs
    }
}
Run Code Online (Sandbox Code Playgroud)

c# constructor

20
推荐指数
2
解决办法
9353
查看次数

Action.ToString()可以返回"System.Action"以外的任何内容吗?

我在Unity中编程,使用Action事件来保存一堆其他Action委托,以便将非Monobehaviour对象挂钩到Update()系统中.我希望能够将操作的名称打印到调试控制台,但使用类似于:

Delegate[] actions = updateActions.GetInvocationList();
foreach ( Delegate del in actions ) {
    Debug.Log( del.ToString() );
}
Run Code Online (Sandbox Code Playgroud)

...只返回"System.Action".我也试过(del as Action).ToString()没有运气.

c# action tostring

3
推荐指数
1
解决办法
2311
查看次数

标签 统计

c# ×2

action ×1

constructor ×1

tostring ×1