【发布时间】:2016-10-24 12:25:08
【问题描述】:
我不知道标题在技术上是否正确,但我试图在不同的时间将不同的字符串传递给同一个方法,让我解释一下:
当用户删除他们的新帐户时,会调用它:
RecordMetric(Constants.AccountDeleted, userId, String.Empty);
将通过此方法传递:
internal static void RecordMetric(Constants Constant, long userid, string data, long? siteid)
{
MetricsSystemService.TrackEvent(Constants.AccountDeleted,
new Dictionary<string, string> { { "userid", userid.ToString() }, { "data", data} });
}
这会将数据发送到应用洞察,让我们知道有多少用户删除了他们的帐户。
但是如果我想通过上面的方法传递一个不同的字符串呢,例如:
RecordMetric(Constants.PasspointReset, userId, String.Empty);
我可以为它创建另一个方法,但这会效率低下,相反我希望它通过与 AccountDeleted 字符串相同的方法传递。
我试过做一个字符串扩展:
internal static class ConstantsString
{
internal static string ToConstantsString(this string input)
{
if (input == "SignupStart") return Constants.SignupStart;
if (input == "SignupEnd") return Constants.SignupEnd;
if (input == "LoginStart") return Constants.LoginStart;
if (input == "LoginEnd") return Constants.LoginEnd;
if (input == "AccountDeleted") return Constants.AccountDeleted;
if (input == "PasspointReset") return Constants.PasspointReset;
if (input == "ImageChange") return Constants.ImageChange;
if (input == "LoginFailed") return Constants.LoginFailed;
if (input == "SignupVerified") return Constants.SignupVerified;
if (input == "LibraryImageUsed") return Constants.LibraryImageUsed;
if (input == "DeveloperAccountCreated") return Constants.DeveloperAccountCreated;
if (input == "DeveloperAccountEdited") return Constants.DeveloperAccountEdited;
if (input == "DeveloperAccountDeleted") return Constants.DeveloperAccountDeleted;
return String.Empty;
}
}
并像这样通过字符串扩展:
MetricsSystemService.TrackEvent(ConstantsString.ToConstantsString(),
new Dictionary<string, string> { { "userid", userid.ToString() }, { "data", data} });
但我收到一个错误:“方法 'ToConstantsString' 没有重载需要 0 个参数。”
还有其他方法可以做到这一点吗?还是我做错了什么?
【问题讨论】:
-
为什么
RecordMetric(Constants.PasspointReset, userId, String.Empty);不起作用?看起来RecordMetric已经接受了string、userid和string参数。 -
看起来你可以在不创建任何新方法的情况下做你想做的事。
RecordMetric(Constants.AccountDeleted, userId, String.Empty);之类的代码应该实际运行。