Refactor C# keyword modifer code slightly

This commit is contained in:
Katy Coe
2019-11-10 17:46:19 +01:00
parent b980798ab5
commit 54d03b9f0f
4 changed files with 85 additions and 68 deletions

View File

@@ -5,7 +5,9 @@
*/
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Il2CppInspector.Reflection {
public class FieldInfo : MemberInfo
@@ -112,5 +114,32 @@ namespace Il2CppInspector.Reflection {
if (pkg.FieldDefaultValue.TryGetValue(fieldIndex, out object variant))
DefaultValue = variant;
}
public string GetAccessModifierString() => this switch {
{ IsPrivate: true } => "private ",
{ IsPublic: true } => "public ",
{ IsFamily: true } => "protected ",
{ IsAssembly: true } => "internal ",
{ IsFamilyOrAssembly: true } => "protected internal ",
{ IsFamilyAndAssembly: true } => "private protected ",
_ => ""
};
public string GetModifierString() {
var modifiers = new StringBuilder(GetAccessModifierString());
if (IsLiteral)
modifiers.Append("const ");
// All const fields are also static by implication
else if (IsStatic)
modifiers.Append("static ");
if (IsInitOnly)
modifiers.Append("readonly ");
if (IsPinvokeImpl)
modifiers.Append("extern ");
if (GetCustomAttributes("System.Runtime.CompilerServices.FixedBufferAttribute").Any())
modifiers.Append("fixed ");
return modifiers.ToString();
}
}
}

View File

@@ -115,26 +115,22 @@ namespace Il2CppInspector.Reflection
DeclaredParameters.Add(new ParameterInfo(pkg, p, this));
}
public string GetAccessModifierString() {
return this switch {
{ IsPrivate: true } => "private ",
{ IsPublic: true } => "public ",
{ IsFamily: true } => "protected ",
{ IsAssembly: true } => "internal ",
{ IsFamilyOrAssembly: true } => "protected internal ",
{ IsFamilyAndAssembly: true } => "private protected ",
_ => ""
};
}
public string GetAccessModifierString() => this switch {
{ IsPrivate: true } => "private ",
{ IsPublic: true } => "public ",
{ IsFamily: true } => "protected ",
{ IsAssembly: true } => "internal ",
{ IsFamilyOrAssembly: true } => "protected internal ",
{ IsFamilyAndAssembly: true } => "private protected ",
_ => ""
};
public string GetModifierString() {
// Interface methods and properties have no visible modifiers (they are always declared 'public abstract')
if (DeclaringType.IsInterface)
return string.Empty;
StringBuilder modifiers = new StringBuilder();
modifiers.Append(GetAccessModifierString());
var modifiers = new StringBuilder(GetAccessModifierString());
if (IsAbstract)
modifiers.Append("abstract ");

View File

@@ -8,6 +8,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Il2CppInspector.Reflection {
public class TypeInfo : MemberInfo
@@ -453,6 +454,43 @@ namespace Il2CppInspector.Reflection {
+ (IsArray ? "[" + new string(',', GetArrayRank() - 1) + "]" : "")
+ (IsPointer ? "*" : "");
public string GetAccessModifierString() => this switch {
{ IsPublic: true } => "public ",
{ IsNotPublic: true } => "internal ",
{ IsNestedPublic: true } => "public ",
{ IsNestedPrivate: true } => "private ",
{ IsNestedFamily: true } => "protected ",
{ IsNestedAssembly: true } => "internal ",
{ IsNestedFamORAssem: true } => "protected internal ",
{ IsNestedFamANDAssem: true } => "private protected ",
_ => throw new InvalidOperationException("Unknown type access modifier")
};
public string GetModifierString() {
var modifiers = new StringBuilder(GetAccessModifierString());
// An abstract sealed class is a static class
if (IsAbstract && IsSealed)
modifiers.Append("static ");
else {
if (IsAbstract && !IsInterface)
modifiers.Append("abstract ");
if (IsSealed && !IsValueType && !IsEnum)
modifiers.Append("sealed ");
}
if (IsInterface)
modifiers.Append("interface ");
else if (IsValueType)
modifiers.Append("struct ");
else if (IsEnum)
modifiers.Append("enum ");
else
modifiers.Append("class ");
return modifiers.ToString();
}
public override string ToString() => Name;
}
}