- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在以爆炸为特色的 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()'
谁能帮我解决这个问题?
有没有办法绕过必须在初始化时执行此操作的事实?
最佳答案
我喜欢字典初始化器,而且我一直都在使用它们。我感觉到你的眼泪。
答案就在你的问题中,使用委托(delegate)!
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 中经过全面测试。
关于c# - 我流泪了,因为我不知道如何用 Action 现场初始化 Dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342919/
我刚接触一个项目,开发人员仍然通过邮件发送文件来共享代码。我们正在使用 eclipse 和 cvs。 场外的开发人员将代码发送到现场进行审核,其他开发人员从那里的邮件中一一获取文件并在 Eclipse
我完成了 session 站点设计,现在应该创建数据库。但我不确定该使用哪种方法。我正在使用 Apache、PHP 和 MySQL。我有用户,每个用户都有输入和输出邮箱,每个用户都有一个好友列表。这是
我们的开发团队刚刚将一个应用程序从本地服务器迁移到实时站点。该应用程序利用可写入的远程数据库,并利用 MSQLi 和 PDO 方法获取数据并将数据推送到数据库。 更新 connect .inc 文件以
大家好,我想使用 java 连接到我网站上的数据库。我想知道我应该在 URL 中输入什么: cnt=DriverManager.getConnection(DB_URL, DB_UID,DB_PWD)
我不确定“硬失败”是否是正确的词,但这是我面临的问题。我花了相当长的时间来重现这个尽可能最小的例子,所以它是这样的: class BaseParameterizedType fun > getSpec
我希望有人能够帮助解决我遇到的问题。下面的代码管理我正在开发的网站上的音频播放器。这个想法是,用户可以单击静音按钮来切换网站上的声音,如果用户单击特定链接离开网站,则声音应该被静音(如果尚未静音)。
我在站点中使用 libcurl 进行身份验证。我用它制作了很多功能,例如: char *CheckLoginPass(char *login, char *pass) { //use curl_e
我正在创建一个社交媒体风格的网站,并使用 Meteor 和 MongoDB 创建帖子,其中包含 {{title}} 和 {{content}}一个 Accordion 菜单,标题在菜单的标题上,内容在
我昨天为一个网站写了一个小的桌面 package 器(它实际上只是加载一个网站),作为对 Electron 的一些练习,但我注意到以下事情: 通常应该(并且确实)在每个浏览器上工作的 Javascri
我的应用程序中有 WebView 。我可以在 IDE 中看到警告。“使用 setJavaScriptEnabled 会引入 XSS 漏洞”代码 webView.getSettings().setJav
我正在做一个多步骤表单,所有内容都在最后保存。在我的模型中,我有一个 m2m 复选框字段,并且我使用 django Sessions 来获取表单数据以在最后一步中显示它。 问题是当我提交最终表单时,m
我们已经实现了 WURFL 现场 Java 应用程序(使用 maven/servlet 实现的 berryapp),我们将其用于所有 Java 应用程序,只需在所有其他应用程序中调用该应用程序即可。由
我是一名优秀的程序员,十分优秀!