/// <summary> /// Gets the ThriftStructMetadata for the specified struct class. The struct class must be annotated with @ThriftStruct or @ThriftUnion. /// </summary> /// <param name="structType"></param> /// <returns></returns> public ThriftStructMetadata GetThriftStructMetadata(Type structType) { return(_structs.GetOrAdd(structType, t => { ThriftStructAttribute att = t.GetTypeInfo().GetCustomAttribute <ThriftStructAttribute>(); if (att != null) { return ExtractThriftStructMetadata(t); } else { throw new ArgumentException($"{structType.FullName} has no ThriftStructAttribute."); } })); }
private IDictionary <short, ThriftType> BuildExceptions(ThriftCatalog catalog, MethodInfo method) { var exceptions = ImmutableDictionary.CreateBuilder <short, ThriftType>(); HashSet <Type> exceptionTypes = new HashSet <Type>(); var exceptionAttributes = method.GetCustomAttributes <ThriftExceptionAttribute>(true); foreach (var thriftException in exceptionAttributes) { if (!exceptionTypes.Add(thriftException.ExceptionType)) { throw new ThriftyException($"ThriftExceptionAttribute on method {method.DeclaringType}.{method.Name} contains more than one value for {thriftException.ExceptionType} ."); } if (exceptions.ContainsKey(thriftException.Id)) { throw new ThriftyException($"ThriftExceptionAttribute on method {method.DeclaringType}.{method.Name} has duplicate id: {thriftException.Id} ."); } exceptions.Add(thriftException.Id, catalog.GetThriftType(thriftException.ExceptionType)); } foreach (var exceptionType in exceptionAttributes.Select(a => a.ExceptionType)) { if (exceptionType.GetTypeInfo().IsAssignableFrom(typeof(TException))) { // the built-in exception types don't need special treatment continue; } ThriftStructAttribute attribute = exceptionType.GetTypeInfo().GetCustomAttribute <ThriftStructAttribute>(); if (attribute == null) { throw new ThriftyException($"ThriftExceptionAttribute on method {method.DeclaringType}.{method.Name} with {exceptionType.FullName} need {nameof(ThriftStructAttribute)} ."); } } return(exceptions.ToImmutableDictionary()); }