Added dynamic method builder for member values
このコミットが含まれているのは:
1 親
a03133e756
コミット
12e56b8a0f
2ファイル変更
+74
-15
検索条件に一致するファイルはありません
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection.Emit;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation.Models
|
||||
{
|
||||
internal sealed partial class ReadableMember
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of members to getter delegates
|
||||
/// </summary>
|
||||
private static readonly Dictionary<string, Getter> GettersMapping = new Dictionary<string, Getter>();
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
/// <param name="instance">The target instance to use to read the value from</param>
|
||||
[Pure]
|
||||
public object GetValue(object? instance)
|
||||
{
|
||||
if (!GettersMapping.TryGetValue(Id, out Getter getter))
|
||||
{
|
||||
getter = BuildDynamicGetter();
|
||||
GettersMapping.Add(Id, getter);
|
||||
}
|
||||
|
||||
return getter(instance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="delegate"/> that represents a getter for a specific member
|
||||
/// </summary>
|
||||
/// <param name="obj">The source object to get the member from</param>
|
||||
/// <returns>The value of the member, upcast to <see cref="object"/></returns>
|
||||
private delegate object Getter(object? obj);
|
||||
|
||||
/// <summary>
|
||||
/// Builds a dynamic IL method to retrieve the value of the current member
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private Getter BuildDynamicGetter()
|
||||
{
|
||||
// Create a new dynamic method for the current member
|
||||
DynamicMethod method = new DynamicMethod($"Get{Id}", typeof(object), new[] { typeof(object) }, DeclaringType);
|
||||
ILGenerator il = method.GetILGenerator();
|
||||
|
||||
// Load the argument (the object instance) and cast it to the right type
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Castclass, DeclaringType);
|
||||
|
||||
// Get the member value with the appropriate method
|
||||
if (Property != null) il.EmitCall(OpCodes.Callvirt, Property.GetMethod, null);
|
||||
else if (Field != null) il.Emit(OpCodes.Ldfld, Field);
|
||||
else throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
|
||||
// Box the value, if needed
|
||||
if (MemberType.IsValueType) il.Emit(OpCodes.Box, MemberType);
|
||||
|
||||
// Return the member value from the top of the evaluation stack
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
// Build and return the delegate
|
||||
return (Getter)method.CreateDelegate(typeof(Getter));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation.Models
|
||||
@@ -7,8 +6,13 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that wraps a readable member of another <see langword="class"/> and provides access to it
|
||||
/// </summary>
|
||||
internal sealed class ReadableMember
|
||||
internal sealed partial class ReadableMember
|
||||
{
|
||||
/// <summary>
|
||||
/// The id of the current member
|
||||
/// </summary>
|
||||
private readonly string Id;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="FieldInfo"/> object wrapped by the current instance, if present
|
||||
/// </summary>
|
||||
@@ -28,6 +32,7 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
{
|
||||
Field = field;
|
||||
Property = property;
|
||||
Id = $"{DeclaringType.FullName}{Type.Delimiter}{Name}".Replace(Type.Delimiter, '_');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -63,19 +68,6 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
/// </summary>
|
||||
public string Name => (Field?.Name ?? Property?.Name) ?? throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
/// <param name="instance">The target instance to use to read the value from</param>
|
||||
[Pure]
|
||||
public object GetValue(object? instance)
|
||||
{
|
||||
if (Field != null) return Field.GetValue(instance);
|
||||
if (Property != null) return Property.GetValue(instance);
|
||||
|
||||
throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="FieldInfo"/> object into a <see cref="ReadableMember"/> instance
|
||||
/// </summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする