Skip to content

Instantly share code, notes, and snippets.

@PascalSenn
Created March 26, 2020 17:28
Show Gist options
  • Save PascalSenn/6a696e0b22730a784adaffe4634b487d to your computer and use it in GitHub Desktop.
Save PascalSenn/6a696e0b22730a784adaffe4634b487d to your computer and use it in GitHub Desktop.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)]
public abstract class DataLoaderAttribute : ObjectFieldDescriptorAttribute
{
public static MethodInfo ConfigureMethod = typeof(DataLoaderAttribute)
.GetMethod(nameof(DataLoaderAttribute.Configure));
public DataLoaderAttribute(Type dataloaderType)
{
DataloaderType = dataloaderType;
}
public Type DataloaderType { get; }
public override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
{
var args = DataloaderType.GetInterface("IDataLoader")?.GetGenericArguments()
?? throw new InvalidOperationException();
ConfigureMethod.MakeGenericMethod(DataloaderType, args[1], args[0])
.Invoke(this, new object[] { context, descriptor, member });
}
public void Configure<TLoader, TPerson, TKey>(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment