Use nameof instead of hardcoded strings in reflection calls.
This helps improve the safety of code the uses reflection when methods may get renamed, and helps navigating code as the nameof will show up when searching for references to members.
This commit is contained in:
@@ -479,7 +479,7 @@ namespace OpenRA
|
||||
|
||||
var parts = value.Split(SplitComma, StringSplitOptions.RemoveEmptyEntries);
|
||||
var arguments = fieldType.GetGenericArguments();
|
||||
var addMethod = fieldType.GetMethod("Add", arguments);
|
||||
var addMethod = fieldType.GetMethod(nameof(List<object>.Add), arguments);
|
||||
var addArgs = new object[1];
|
||||
for (var i = 0; i < parts.Length; i++)
|
||||
{
|
||||
@@ -494,7 +494,7 @@ namespace OpenRA
|
||||
{
|
||||
var dict = Activator.CreateInstance(fieldType);
|
||||
var arguments = fieldType.GetGenericArguments();
|
||||
var addMethod = fieldType.GetMethod("Add", arguments);
|
||||
var addMethod = fieldType.GetMethod(nameof(Dictionary<object, object>.Add), arguments);
|
||||
var addArgs = new object[2];
|
||||
foreach (var node in yaml.Nodes)
|
||||
{
|
||||
|
||||
@@ -248,7 +248,7 @@ namespace OpenRA
|
||||
{
|
||||
var traitName = traitNode.Key.Split('@')[0];
|
||||
var traitType = modData.ObjectCreator.FindType(traitName + "Info");
|
||||
if (traitType != null && traitType.GetInterface("ILobbyCustomRulesIgnore") == null)
|
||||
if (traitType != null && traitType.GetInterface(nameof(ILobbyCustomRulesIgnore)) == null)
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace OpenRA
|
||||
|
||||
var init = (ActorInit)FormatterServices.GetUninitializedObject(type);
|
||||
if (initInstance.Length > 1)
|
||||
type.GetField("InstanceName").SetValue(init, initInstance[1]);
|
||||
type.GetField(nameof(ActorInit.InstanceName)).SetValue(init, initInstance[1]);
|
||||
|
||||
var loader = type.GetMethod("Initialize", new[] { typeof(MiniYaml) });
|
||||
if (loader == null)
|
||||
|
||||
@@ -238,7 +238,7 @@ namespace OpenRA.Network
|
||||
{
|
||||
// The lambda generated is shown below.
|
||||
// TSync is actual type of the ISync object. Foo is a field or property with the Sync attribute applied.
|
||||
var toString = memberType.GetMethod("ToString", Type.EmptyTypes);
|
||||
var toString = memberType.GetMethod(nameof(object.ToString), Type.EmptyTypes);
|
||||
Expression getString;
|
||||
if (memberType.IsValueType)
|
||||
{
|
||||
|
||||
@@ -287,7 +287,7 @@ namespace OpenRA.Scripting
|
||||
|
||||
Type[] FilterCommands(ActorInfo ai, Type[] knownCommands)
|
||||
{
|
||||
var method = typeof(ActorInfo).GetMethod("HasTraitInfo");
|
||||
var method = typeof(ActorInfo).GetMethod(nameof(ActorInfo.HasTraitInfo));
|
||||
return knownCommands.Where(c => ExtractRequiredTypes(c)
|
||||
.All(t => (bool)method.MakeGenericMethod(t).Invoke(ai, NoArguments)))
|
||||
.ToArray();
|
||||
|
||||
Reference in New Issue
Block a user