コード例 #1
0
        public static Task SendCommand <T>(this IPipe <CommandContext> pipe, object values)
            where T : class
        {
            if (pipe == null)
            {
                throw new ArgumentNullException(nameof(pipe));
            }
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var command = TypeNameCache <T> .InitializeFromObject(values);

            var context = new Command <T>(command);

            return(pipe.Send(context));
        }
コード例 #2
0
        Type CreateTypeFromInterface(ModuleBuilder builder, Type interfaceType)
        {
            var typeName = _proxyNamespaceSuffix + "." +
                           (interfaceType.IsNested && interfaceType.DeclaringType != null
                    ? (interfaceType.DeclaringType.Name + '+' + TypeNameCache.GetShortName(interfaceType))
                    : TypeNameCache.GetShortName(interfaceType));

            try
            {
                var typeBuilder = builder.DefineType(typeName,
                                                     TypeAttributes.Serializable | TypeAttributes.Class |
                                                     TypeAttributes.Public | TypeAttributes.Sealed,
                                                     typeof(object), new[] { interfaceType });

                typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

                IEnumerable <PropertyInfo> properties = interfaceType.GetAllProperties();
                foreach (var property in properties)
                {
                    var fieldBuilder = typeBuilder.DefineField("field_" + property.Name, property.PropertyType,
                                                               FieldAttributes.Private);

                    var propertyBuilder = typeBuilder.DefineProperty(property.Name,
                                                                     property.Attributes | PropertyAttributes.HasDefault, property.PropertyType, null);

                    var getMethod = GetGetMethodBuilder(property, typeBuilder, fieldBuilder);
                    var setMethod = GetSetMethodBuilder(property, typeBuilder, fieldBuilder);

                    propertyBuilder.SetGetMethod(getMethod);
                    propertyBuilder.SetSetMethod(setMethod);
                }

                return(typeBuilder.CreateType());
            }
            catch (Exception ex)
            {
                string message = $"Exception creating proxy ({typeName}) for {TypeNameCache.GetShortName(interfaceType)}";

                throw new InvalidOperationException(message, ex);
            }
        }