public static object GetInterfacePayloadTypeTable(Type interfaceType, PayloadTableKind kind) { // find payload table type var needGenericConstruction = (interfaceType.IsGenericType && interfaceType.IsGenericTypeDefinition == false); var definitionType = needGenericConstruction ? interfaceType.GetGenericTypeDefinition() : interfaceType; var payloadTableType = interfaceType.Assembly.GetTypes() .Where(t => { var attr = t.GetCustomAttribute <PayloadTableAttribute>(); return(attr != null && attr.Type == definitionType && attr.Kind == kind); }) .FirstOrDefault(); if (payloadTableType == null) { throw new InvalidOperationException( $"Cannot find payload table class for {interfaceType.FullName}"); } if (needGenericConstruction) { payloadTableType = payloadTableType.MakeGenericType(interfaceType.GetGenericArguments()); } // get table from calling GetPayloadTypes var queryMethodInfo = payloadTableType.GetMethod("GetPayloadTypes"); if (queryMethodInfo == null) { throw new InvalidOperationException( $"Cannot find {payloadTableType.FullName}.GetPayloadTypes()"); } var value = queryMethodInfo.Invoke(null, new object[] { }); if (value == null) { throw new InvalidOperationException( $"Invalid null from {payloadTableType.FullName}.GetPayloadTypes()"); } return(value); }
public static object GetInterfacePayloadTypeTable(Type interfaceType, PayloadTableKind kind) { // find payload table type var needGenericConstruction = (interfaceType.IsGenericType && interfaceType.IsGenericTypeDefinition == false); var definitionType = needGenericConstruction ? interfaceType.GetGenericTypeDefinition() : interfaceType; var payloadTableType = interfaceType.Assembly.GetTypes() .Where(t => { var attr = t.GetCustomAttribute<PayloadTableAttribute>(); return (attr != null && attr.Type == definitionType && attr.Kind == kind); }) .FirstOrDefault(); if (payloadTableType == null) { throw new InvalidOperationException( $"Cannot find payload table class for {interfaceType.FullName}"); } if (needGenericConstruction) { payloadTableType = payloadTableType.MakeGenericType(interfaceType.GetGenericArguments()); } // get table from calling GetPayloadTypes var queryMethodInfo = payloadTableType.GetMethod("GetPayloadTypes"); if (queryMethodInfo == null) { throw new InvalidOperationException( $"Cannot find {payloadTableType.FullName}.GetPayloadTypes()"); } var value = queryMethodInfo.Invoke(null, new object[] { }); if (value == null) { throw new InvalidOperationException( $"Invalid null from {payloadTableType.FullName}.GetPayloadTypes()"); } return value; }
public PayloadTableAttribute(Type type, PayloadTableKind kind) { Type = type; Kind = kind; }