Akka.Interfaced.HandlerBuilderHelpers.GetInterfacePayloadTypeTable C# (CSharp) Method

GetInterfacePayloadTypeTable() public static method

public static GetInterfacePayloadTypeTable ( Type interfaceType, PayloadTableKind kind ) : object
interfaceType System.Type
kind PayloadTableKind
return object
        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;
        }
    }

Usage Example

Beispiel #1
0
        private static Type[] GetInterfacePayloadTypeTable(Type interfaceType)
        {
            var payloadTypes = (Type[])HandlerBuilderHelpers.GetInterfacePayloadTypeTable(interfaceType, PayloadTableKind.Notification);

            if (payloadTypes == null || payloadTypes.GetLength(0) != interfaceType.GetMethods().Length)
            {
                throw new InvalidOperationException(
                          $"Mismatched a payload table for {interfaceType.FullName}");
            }

            return(payloadTypes);
        }