Esempio n. 1
0
        public static void InitializeTypeMappings(IServiceProvider ServiceProvider)
        {
            GrainReferenceConverter = ServiceProvider.GetRequiredService <IGrainReferenceConverter>();
            AsReferenceMethodInfo   = typeof(GrainExtensions).GetMethod("AsReference", new Type[] { typeof(IAddressable) });

            CustomTypeRegistry.AddTypeMapping(typeof(DateTime), typeof(long), false);
            CustomTypeRegistry.AddTypeMapping(typeof(DateTime?), typeof(long?), false);
            CustomTypeRegistry.AddTypeMapping(typeof(DateTime[]), typeof(long[]), false);
            CustomTypeRegistry.AddTypeMapping(typeof(IAddressable), typeof(nullable <blob>), true);
            CustomTypeRegistry.AddTypeMapping(typeof(Guid?), typeof(nullable <blob>), false);
            CustomTypeRegistry.AddTypeMapping(typeof(Guid), typeof(blob), false);
            CustomTypeRegistry.AddTypeMapping(typeof(Guid[]), typeof(blob[]), false);

            // Automatically resolve IGenericSerializable fields or fields with GenericSerializationAttribute to blobs
            CustomTypeRegistry.RegisterResolutionCallback(ResolveSchemaField);

            // Globally register this as a type converter for all types
            CustomTypeRegistry.RegisterTypeConverter(typeof(BondTypeAliasConverter));
        }
        public Expression Convert(Expression value, Type type)
        {
            if (type == typeof(Tag.blob))
            {
                type = typeof(ArraySegment <byte>);
            }

            if (type == typeof(Tag.wstring))
            {
                type = typeof(string);
            }

            if (type != value.Type &&
                value.Type.IsGenericType() &&
                value.Type.GetGenericTypeDefinition() == typeof(Nullable <>) &&
                !(type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                value = Expression.Convert(value, value.Type.GetTypeInfo().GenericTypeArguments[0]);
            }

            if (type != value.Type)
            {
                // Converter can be defined either in the namespace/assembly of one of the types
                // being converted or the namespace/assembly of the schema where the alias is used.
                var all = new[] { type, value.Type }.Select(GetConverter).Where(c => c != null).Concat(converters).Concat(CustomTypeRegistry.GetTypeConverters());

                foreach (var converter in all)
                {
                    var convert = converter.ResolveMethod("Convert", value.Type, type, typeof(Type));
                    if (convert != null)
                    {
                        return(Expression.Call(null, convert, value, Expression.Default(type), Expression.Constant(type)));
                    }
                }
            }

            return(value);
        }