Code refactoring
このコミットが含まれているのは:
1 親
533f07f10e
コミット
a18baed6fe
3ファイル変更
+47
-21
検索条件に一致するファイルはありません
@@ -7,6 +7,35 @@ namespace System.Reflection.Emit
|
||||
/// </summary>
|
||||
internal static class ILGeneratorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Puts the appropriate <see langword="unbox"/> or <see langword="castclass"/> instruction to unbox/cast a value onto the stream of instructions
|
||||
/// </summary>
|
||||
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions</param>
|
||||
/// <param name="type">The type of value to convert</param>
|
||||
public static void EmitCastOrUnbox(this ILGenerator il, Type type)
|
||||
{
|
||||
il.Emit(type.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts the appropriate <see langword="ldsfld"/>, <see langword="ldfld"/>, <see langword="call"/> or <see langword="callvirt"/> instruction to read a member onte the stream of instructions
|
||||
/// </summary>
|
||||
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions</param>
|
||||
/// <param name="member">The member to read</param>
|
||||
public static void EmitReadMember(this ILGenerator il, MemberInfo member)
|
||||
{
|
||||
switch (member)
|
||||
{
|
||||
case FieldInfo field:
|
||||
il.Emit(field.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, field);
|
||||
break;
|
||||
case PropertyInfo property when property.CanRead:
|
||||
il.EmitCall(property.GetMethod.IsStatic ? OpCodes.Call : OpCodes.Callvirt, property.GetMethod, null);
|
||||
break;
|
||||
default: throw new ArgumentException($"The input {member.GetType()} instance can't be read");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts the appropriate <see langword="ldc.i4"/>, <see langword="conv.i"/> and <see langword="add"/> instructions to advance a reference onto the stream of instructions
|
||||
/// </summary>
|
||||
|
||||
@@ -88,18 +88,13 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
if (!IsStatic)
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
|
||||
Type unboxType = hierarchy[0].DeclaringType;
|
||||
il.Emit(unboxType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, unboxType);
|
||||
il.EmitCastOrUnbox(hierarchy[0].DeclaringType);
|
||||
}
|
||||
|
||||
// Unroll the member access
|
||||
foreach (ReadableMember member in hierarchy)
|
||||
{
|
||||
// Get the member value with the appropriate method
|
||||
if (member.Property != null) il.EmitCall(member.IsStatic ? OpCodes.Call : OpCodes.Callvirt, member.Property.GetMethod, null);
|
||||
else if (member.Field != null) il.Emit(member.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, member.Field);
|
||||
else throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
il.EmitReadMember(member);
|
||||
}
|
||||
|
||||
// Box the value, if needed
|
||||
|
||||
@@ -14,14 +14,9 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
private readonly string Id;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="FieldInfo"/> object wrapped by the current instance, if present
|
||||
/// The <see cref="MemberInfo"/> object wrapped by the current instance
|
||||
/// </summary>
|
||||
private readonly FieldInfo? Field;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="PropertyInfo"/> object wrapped by the current instance, if present
|
||||
/// </summary>
|
||||
private readonly PropertyInfo? Property;
|
||||
private readonly MemberInfo Member;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadableMember"/> instance with the given parameters
|
||||
@@ -30,22 +25,23 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
private ReadableMember(MemberInfo memberInfo)
|
||||
{
|
||||
// General properties
|
||||
Id = $"{memberInfo.DeclaringType.FullName}{Type.Delimiter}{memberInfo.Name}";
|
||||
Member = memberInfo;
|
||||
Name = memberInfo.Name;
|
||||
Id = $"{memberInfo.DeclaringType.FullName}{Type.Delimiter}{Name}";
|
||||
DeclaringType = memberInfo.DeclaringType;
|
||||
|
||||
// Type specific properties
|
||||
if (memberInfo is FieldInfo fieldInfo)
|
||||
{
|
||||
Field = fieldInfo;
|
||||
IsStatic = Field.IsStatic;
|
||||
MemberType = Field.FieldType;
|
||||
Member = fieldInfo;
|
||||
IsStatic = fieldInfo.IsStatic;
|
||||
MemberType = fieldInfo.FieldType;
|
||||
}
|
||||
else if (memberInfo is PropertyInfo propertyInfo)
|
||||
{
|
||||
Property = propertyInfo;
|
||||
IsStatic = Property.GetMethod.IsStatic;
|
||||
MemberType = Property.PropertyType;
|
||||
Member = propertyInfo;
|
||||
IsStatic = propertyInfo.GetMethod.IsStatic;
|
||||
MemberType = propertyInfo.PropertyType;
|
||||
}
|
||||
else throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
}
|
||||
@@ -81,5 +77,11 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
/// </summary>
|
||||
/// <param name="property">The input <see cref="PropertyInfo"/> object to wrap</param>
|
||||
public static implicit operator ReadableMember(PropertyInfo property) => new ReadableMember(property);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="ReadableMember"/> object into a <see cref="MemberInfo"/> instance
|
||||
/// </summary>
|
||||
/// <param name="member">The input <see cref="ReadableMember"/> object to unwrap</param>
|
||||
public static implicit operator MemberInfo(ReadableMember member) => member.Member;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする