我流泪了,因为我不知道如何用 Action 现场初始化 Dictionary?
I have tears because I don't know how to field initialize a Dictionary with Action?
在以爆炸为特色的 Unity 项目中,您经常这样做
private Dictionary<string, System.Action> explosions;
void .. initialize in your class constructor .. ()
{
explosions = new Dictionary<string, System.Action>();
explosions.Add("huge", SomeCall);
explosions.Add("huger", SomeCall);
etc
}
没问题,但如果你能做到这一点我会很高兴...
private Dictionary<string, System.Action> explosions =
new Dictionary<string, System.Action>()
{
{"huge", SomeCall},
{"huge", SomeCall},
etc
};
那会让我更快乐......你知道当你睡得更好,享受更多微笑时的感觉吗?
当然,那是行不通的,因为:
Assets/scripts/objects/flite groups/ReallyTremendousExplosions.cs(134,26): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `ReallyTremendousExplosions.SomeCall()'
谁能帮我解决这个问题?
有没有办法解决必须在初始化时执行此操作的事实?
似乎 SomeCall
是一个 非静态 方法,这就是为什么你不能在 字段初始值设定项[=17= 中使用它的原因].但是,您可以将初始化放入 constructor:
public class MyClass {
...
// non static method
private void SomeCall() { ... }
private Dictionary<string, System.Action> explosions;
public MyClass() {
explosions = new Dictionary<string, System.Action>() {
{"huge", SomeCall},
{"huger", SomeCall},
};
}
...
}
我喜欢字典初始化程序,而且我一直都在使用它们。我感觉到你的眼泪。
答案在你的问题中,使用委托!
public class ReallyTremendousExplosions
{
private delegate void ExplosionFactory(ReallyTremendousExplosions source);
private Dictionary<string, ExplosionFactory> Explosions = new Dictionary<string, ExplosionFactory>()
{
{ "huge", x => x.SomeMethod() },
{ "huger", x => x.SomeMethod() }
};
private void SomeMethod()
{
//Render massive explosions...
}
public void RenderNamedExplosion(string explosionName) {
ExplosionsFactory explosionFactory;
if (!Explosions.TryGet(explosionName, out explosionFactory) {
throw new NoSuchExplosionException(explosionName);
}
explosionFactory(this);
}
}
代码在Unity5/Mono.
中经过全面测试
在以爆炸为特色的 Unity 项目中,您经常这样做
private Dictionary<string, System.Action> explosions;
void .. initialize in your class constructor .. ()
{
explosions = new Dictionary<string, System.Action>();
explosions.Add("huge", SomeCall);
explosions.Add("huger", SomeCall);
etc
}
没问题,但如果你能做到这一点我会很高兴...
private Dictionary<string, System.Action> explosions =
new Dictionary<string, System.Action>()
{
{"huge", SomeCall},
{"huge", SomeCall},
etc
};
那会让我更快乐......你知道当你睡得更好,享受更多微笑时的感觉吗?
当然,那是行不通的,因为:
Assets/scripts/objects/flite groups/ReallyTremendousExplosions.cs(134,26): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `ReallyTremendousExplosions.SomeCall()'
谁能帮我解决这个问题?
有没有办法解决必须在初始化时执行此操作的事实?
似乎 SomeCall
是一个 非静态 方法,这就是为什么你不能在 字段初始值设定项[=17= 中使用它的原因].但是,您可以将初始化放入 constructor:
public class MyClass {
...
// non static method
private void SomeCall() { ... }
private Dictionary<string, System.Action> explosions;
public MyClass() {
explosions = new Dictionary<string, System.Action>() {
{"huge", SomeCall},
{"huger", SomeCall},
};
}
...
}
我喜欢字典初始化程序,而且我一直都在使用它们。我感觉到你的眼泪。
答案在你的问题中,使用委托!
public class ReallyTremendousExplosions
{
private delegate void ExplosionFactory(ReallyTremendousExplosions source);
private Dictionary<string, ExplosionFactory> Explosions = new Dictionary<string, ExplosionFactory>()
{
{ "huge", x => x.SomeMethod() },
{ "huger", x => x.SomeMethod() }
};
private void SomeMethod()
{
//Render massive explosions...
}
public void RenderNamedExplosion(string explosionName) {
ExplosionsFactory explosionFactory;
if (!Explosions.TryGet(explosionName, out explosionFactory) {
throw new NoSuchExplosionException(explosionName);
}
explosionFactory(this);
}
}
代码在Unity5/Mono.
中经过全面测试