Skip to content

Commit

Permalink
#2 Use var everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
sguldmund committed Jan 12, 2024
1 parent ccb773a commit bd401d2
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/Pose/Extensions/ILGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class ILGeneratorExtensions
public static byte[] GetILBytes(this ILGenerator ilGenerator)
{
var bakeByteArray = typeof(ILGenerator).GetMethod("BakeByteArray", BindingFlags.Instance | BindingFlags.NonPublic);
byte[] ilBytes = (byte[])bakeByteArray.Invoke(ilGenerator, null);
var ilBytes = (byte[])bakeByteArray.Invoke(ilGenerator, null);
return ilBytes;
}
}
Expand Down
40 changes: 20 additions & 20 deletions src/Pose/Helpers/ShimHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public static MethodBase GetMethodFromExpression(Expression expression, bool set
{
case ExpressionType.MemberAccess:
{
MemberExpression memberExpression = expression as MemberExpression;
MemberInfo memberInfo = memberExpression.Member;
var memberExpression = expression as MemberExpression;
var memberInfo = memberExpression.Member;
if (memberInfo.MemberType == MemberTypes.Property)
{
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
var propertyInfo = memberInfo as PropertyInfo;
instanceOrType = GetObjectInstanceOrType(memberExpression.Expression);
return setter ? propertyInfo.GetSetMethod() : propertyInfo.GetGetMethod();
}
else
throw new NotImplementedException("Unsupported expression");
}
case ExpressionType.Call:
MethodCallExpression methodCallExpression = expression as MethodCallExpression;
var methodCallExpression = expression as MethodCallExpression;
instanceOrType = GetObjectInstanceOrType(methodCallExpression.Object);
return methodCallExpression.Method;
case ExpressionType.New:
NewExpression newExpression = expression as NewExpression;
var newExpression = expression as NewExpression;
instanceOrType = null;
return newExpression.Constructor;
default:
Expand All @@ -43,17 +43,17 @@ public static MethodBase GetMethodFromExpression(Expression expression, bool set

public static void ValidateReplacementMethodSignature(MethodBase original, MethodInfo replacement, Type type, bool setter)
{
bool isValueType = original.DeclaringType.IsValueType;
bool isStatic = original.IsStatic;
bool isConstructor = original.IsConstructor;
bool isStaticOrConstructor = isStatic || isConstructor;
var isValueType = original.DeclaringType.IsValueType;
var isStatic = original.IsStatic;
var isConstructor = original.IsConstructor;
var isStaticOrConstructor = isStatic || isConstructor;

Type vaildReturnType = isConstructor ? original.DeclaringType : (original as MethodInfo).ReturnType;
var vaildReturnType = isConstructor ? original.DeclaringType : (original as MethodInfo).ReturnType;
vaildReturnType = setter ? typeof(void) : vaildReturnType;
Type shimReturnType = replacement.ReturnType;
var shimReturnType = replacement.ReturnType;

Type validOwningType = type;
Type shimOwningType = isStaticOrConstructor
var validOwningType = type;
var shimOwningType = isStaticOrConstructor
? validOwningType : replacement.GetParameters().Select(p => p.ParameterType).FirstOrDefault();

var validParameterTypes = original.GetParameters().Select(p => p.ParameterType);
Expand All @@ -76,7 +76,7 @@ public static void ValidateReplacementMethodSignature(MethodBase original, Metho
if (validParameterTypes.Count() != shimParameterTypes.Count())
throw new InvalidShimSignatureException("Parameters count do not match");

for (int i = 0; i < validParameterTypes.Count(); i++)
for (var i = 0; i < validParameterTypes.Count(); i++)
{
if (validParameterTypes.ElementAt(i) != shimParameterTypes.ElementAt(i))
throw new InvalidShimSignatureException($"Parameter types at {i} do not match");
Expand All @@ -90,17 +90,17 @@ public static object GetObjectInstanceOrType(Expression expression)
{
case ExpressionType.MemberAccess:
{
MemberExpression memberExpression = expression as MemberExpression;
ConstantExpression constantExpression = memberExpression.Expression as ConstantExpression;
var memberExpression = expression as MemberExpression;
var constantExpression = memberExpression.Expression as ConstantExpression;
if (memberExpression.Member.MemberType == MemberTypes.Field)
{
FieldInfo fieldInfo = (memberExpression.Member as FieldInfo);
var fieldInfo = (memberExpression.Member as FieldInfo);
var obj = fieldInfo.IsStatic ? null : constantExpression.Value;
instanceOrType = fieldInfo.GetValue(obj);
}
else if (memberExpression.Member.MemberType == MemberTypes.Property)
{
PropertyInfo propertyInfo = (memberExpression.Member as PropertyInfo);
var propertyInfo = (memberExpression.Member as PropertyInfo);
var obj = propertyInfo.GetMethod.IsStatic ? null : constantExpression.Value;
instanceOrType = propertyInfo.GetValue(obj);
}
Expand All @@ -109,8 +109,8 @@ public static object GetObjectInstanceOrType(Expression expression)
}
case ExpressionType.Call:
{
MethodCallExpression methodCallExpression = expression as MethodCallExpression;
MethodInfo methodInfo = methodCallExpression.Method;
var methodCallExpression = expression as MethodCallExpression;
var methodInfo = methodCallExpression.Method;
instanceOrType = methodInfo.GetGenericArguments().FirstOrDefault();
break;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pose/Helpers/StubHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static int GetIndexOfMatchingShim(MethodBase methodBase, Type type, objec
if (methodBase.IsStatic || obj == null)
return Array.FindIndex(PoseContext.Shims, s => s.Original == methodBase);

int index = Array.FindIndex(PoseContext.Shims,
var index = Array.FindIndex(PoseContext.Shims,
s => Object.ReferenceEquals(obj, s.Instance) && s.Original == methodBase);

if (index == -1)
Expand All @@ -52,8 +52,8 @@ public static MethodInfo DevirtualizeMethod(object obj, MethodInfo virtualMethod
public static MethodInfo DevirtualizeMethod(Type thisType, MethodInfo virtualMethod)
{
if (thisType == virtualMethod.DeclaringType) return virtualMethod;
BindingFlags bindingFlags = BindingFlags.Instance | (virtualMethod.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic);
Type[] types = virtualMethod.GetParameters().Select(p => p.ParameterType).ToArray();
var bindingFlags = BindingFlags.Instance | (virtualMethod.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic);
var types = virtualMethod.GetParameters().Select(p => p.ParameterType).ToArray();
return thisType.GetMethod(virtualMethod.Name, bindingFlags, null, types, null);
}

Expand All @@ -68,7 +68,7 @@ public static bool IsIntrinsic(MethodBase method)

public static string CreateStubNameFromMethod(string prefix, MethodBase method)
{
string name = prefix;
var name = prefix;
name += "_";
name += method.DeclaringType.ToString();
name += "_";
Expand Down
32 changes: 16 additions & 16 deletions src/Pose/IL/MethodRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public static MethodRewriter CreateRewriter(MethodBase method, bool isInterfaceD

public MethodBase Rewrite()
{
List<Type> parameterTypes = new List<Type>();
var parameterTypes = new List<Type>();
if (!m_method.IsStatic)
{
Type thisType = m_isInterfaceDispatch ? typeof(object) : m_owningType;
var thisType = m_isInterfaceDispatch ? typeof(object) : m_owningType;
if (!m_isInterfaceDispatch && m_owningType.IsValueType)
{
thisType = thisType.MakeByRefType();
Expand All @@ -48,9 +48,9 @@ public MethodBase Rewrite()
}

parameterTypes.AddRange(m_method.GetParameters().Select(p => p.ParameterType));
Type returnType = m_method.IsConstructor ? typeof(void) : (m_method as MethodInfo).ReturnType;
var returnType = m_method.IsConstructor ? typeof(void) : (m_method as MethodInfo).ReturnType;

DynamicMethod dynamicMethod = new DynamicMethod(
var dynamicMethod = new DynamicMethod(
StubHelper.CreateStubNameFromMethod("impl", m_method),
returnType,
parameterTypes.ToArray(),
Expand All @@ -67,7 +67,7 @@ public MethodBase Rewrite()

foreach (var clause in methodBody.ExceptionHandlingClauses)
{
ExceptionHandler handler = new ExceptionHandler();
var handler = new ExceptionHandler();
handler.Flags = clause.Flags;
handler.CatchType = clause.Flags == ExceptionHandlingClauseOptions.Clause ? clause.CatchType : null;
handler.TryStart = clause.TryOffset;
Expand All @@ -85,16 +85,16 @@ public MethodBase Rewrite()
.Where(i => (i.Operand as Instruction) != null)
.Select(i => (i.Operand as Instruction));

foreach (Instruction instruction in ifTargets)
foreach (var instruction in ifTargets)
targetInstructions.TryAdd(instruction.Offset, ilGenerator.DefineLabel());

var switchTargets = instructions
.Where(i => (i.Operand as Instruction[]) != null)
.Select(i => (i.Operand as Instruction[]));

foreach (Instruction[] _instructions in switchTargets)
foreach (var _instructions in switchTargets)
{
foreach (Instruction _instruction in _instructions)
foreach (var _instruction in _instructions)
targetInstructions.TryAdd(_instruction.Offset, ilGenerator.DefineLabel());
}

Expand All @@ -110,7 +110,7 @@ public MethodBase Rewrite()

EmitILForExceptionHandlers(ilGenerator, instruction, handlers);

if (targetInstructions.TryGetValue(instruction.Offset, out Label label))
if (targetInstructions.TryGetValue(instruction.Offset, out var label))
ilGenerator.MarkLabel(label);

if (s_IngoredOpCodes.Contains(instruction.OpCode)) continue;
Expand Down Expand Up @@ -268,9 +268,9 @@ private void EmitILForInlineString(ILGenerator ilGenerator, Instruction instruct
private void EmitILForInlineBrTarget(ILGenerator ilGenerator,
Instruction instruction, Dictionary<int, Label> targetInstructions)
{
Label targetLabel = targetInstructions[(instruction.Operand as Instruction).Offset];
var targetLabel = targetInstructions[(instruction.Operand as Instruction).Offset];

OpCode opCode = instruction.OpCode;
var opCode = instruction.OpCode;

// Offset values could change and not be short form anymore
if (opCode == OpCodes.Br_S) opCode = OpCodes.Br;
Expand Down Expand Up @@ -298,16 +298,16 @@ private void EmitILForInlineString(ILGenerator ilGenerator, Instruction instruct
private void EmitILForInlineSwitch(ILGenerator ilGenerator,
Instruction instruction, Dictionary<int, Label> targetInstructions)
{
Instruction[] switchInstructions = (Instruction[])instruction.Operand;
Label[] targetLabels = new Label[switchInstructions.Length];
for (int i = 0; i < switchInstructions.Length; i++)
var switchInstructions = (Instruction[])instruction.Operand;
var targetLabels = new Label[switchInstructions.Length];
for (var i = 0; i < switchInstructions.Length; i++)
targetLabels[i] = targetInstructions[switchInstructions[i].Offset];
ilGenerator.Emit(instruction.OpCode, targetLabels);
}

private void EmitILForInlineVar(ILGenerator ilGenerator, Instruction instruction)
{
int index = 0;
var index = 0;
if (instruction.OpCode.Name.Contains("loc"))
{
index = ((LocalVariableInfo)instruction.Operand).LocalIndex;
Expand Down Expand Up @@ -424,7 +424,7 @@ private void EmitILForMethod(ILGenerator ilGenerator, Instruction instruction, M

private void EmitILForInlineMember(ILGenerator ilGenerator, Instruction instruction)
{
MemberInfo memberInfo = (MemberInfo)instruction.Operand;
var memberInfo = (MemberInfo)instruction.Operand;
if (memberInfo.MemberType == MemberTypes.Field)
{
ilGenerator.Emit(instruction.OpCode, memberInfo as FieldInfo);
Expand Down
Loading

0 comments on commit bd401d2

Please sign in to comment.