public object Cast(IAddressable grain, Type interfaceType) { GrainReferenceCaster caster; if (!this.casters.TryGetValue(interfaceType, out caster)) { // Create and cache a caster for the interface type. caster = this.casters.GetOrAdd(interfaceType, MakeCaster); } return(caster(grain)); GrainReferenceCaster MakeCaster(Type interfaceType) { var grainReferenceType = this.typeCache.GetGrainReferenceType(interfaceType); return(GrainCasterFactory.CreateGrainReferenceCaster(interfaceType, grainReferenceType)); } }
private static GrainReferenceCaster MakeCaster(Type interfaceType) { var typeInfo = interfaceType.GetTypeInfo(); CodeGeneratorManager.GenerateAndCacheCodeForAssembly(typeInfo.Assembly); var genericInterfaceType = interfaceType.IsConstructedGenericType ? typeInfo.GetGenericTypeDefinition() : interfaceType; if (!typeof(IAddressable).IsAssignableFrom(interfaceType)) { throw new InvalidCastException( $"Target interface must be derived from Orleans.IAddressable - cannot handle {interfaceType}"); } // Try to find the correct GrainReference type for this interface. Type grainReferenceType; if (!GrainToReferenceMapping.TryGetValue(genericInterfaceType, out grainReferenceType)) { throw new InvalidOperationException( $"Cannot find generated GrainReference class for interface '{interfaceType}'"); } if (interfaceType.IsConstructedGenericType) { grainReferenceType = grainReferenceType.MakeGenericType(typeInfo.GenericTypeArguments); } if (!typeof(IAddressable).IsAssignableFrom(grainReferenceType)) { // This represents an internal programming error. throw new InvalidCastException( $"Target reference type must be derived from Orleans.IAddressable - cannot handle {grainReferenceType}"); } return(GrainCasterFactory.CreateGrainReferenceCaster(interfaceType, grainReferenceType)); }
/// <summary> /// Creates and returns a new grain reference caster. /// </summary> /// <param name="interfaceType">The interface which the result will cast to.</param> /// <returns>A new grain reference caster.</returns> private GrainReferenceCaster MakeCaster(Type interfaceType) { var grainReferenceType = this.typeCache.GetGrainReferenceType(interfaceType); return(GrainCasterFactory.CreateGrainReferenceCaster(interfaceType, grainReferenceType)); }