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:
RoosterDragon
2022-04-04 16:08:55 +01:00
committed by abcdefg30
parent b254eb0f3d
commit ac0969d688
8 changed files with 9 additions and 9 deletions

View File

@@ -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)
{

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)
{

View File

@@ -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();

View File

@@ -97,7 +97,7 @@ namespace OpenRA.Mods.Cnc.Traits
// work around the cancellation bug.
// HACK: this is manipulating private internal actor state
if (self.CurrentActivity is Move)
typeof(Actor).GetProperty("CurrentActivity").SetValue(self, null);
typeof(Actor).GetProperty(nameof(Actor.CurrentActivity)).SetValue(self, null);
// The actor is killed using Info.DamageTypes if the teleport fails
self.QueueActivity(false, new Teleport(chronosphere ?? self, Origin, null, true, killCargo, Info.ChronoshiftSound,

View File

@@ -76,7 +76,7 @@ namespace OpenRA.Mods.Common
// HACK: We need to set HasRegisteredUriScheme to bypass the check that is done when calling SetPresence with a joinSecret.
// DiscordRpc lib expect us to register uri handlers with RegisterUriScheme(), we are doing it ourselves in our installers/launchers.
client.GetType().GetProperty("HasRegisteredUriScheme").SetValue(client, true);
client.GetType().GetProperty(nameof(DiscordRpcClient.HasRegisteredUriScheme)).SetValue(client, true);
client.SetSubscription(EventType.Join | EventType.JoinRequest);
client.Initialize();

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Scripting
// Construct the ActorInit.
var init = (ActorInit)FormatterServices.GetUninitializedObject(initType);
if (initInstance.Length > 1)
initType.GetField("InstanceName").SetValue(init, initInstance[1]);
initType.GetField(nameof(ActorInit.InstanceName)).SetValue(init, initInstance[1]);
if (value is LuaTable tableValue && init is CompositeActorInit compositeInit)
{