NServiceBus.DelegateFactory.CreateSet C# (CSharp) Method

CreateSet() public static method

public static CreateSet ( FieldInfo field ) : object>.Action
field System.Reflection.FieldInfo
return object>.Action
        public static Action<object, object> CreateSet(FieldInfo field)
        {
            Action<object, object> callback;

            if (!FieldInfoToLateBoundFieldSet.TryGetValue(field, out callback))
            {
                var sourceType = field.DeclaringType;
                var method = new DynamicMethod("Set" + field.Name, null, new[]
                {
                    typeof(object),
                    typeof(object)
                }, true);
                var gen = method.GetILGenerator();

                gen.Emit(OpCodes.Ldarg_0); // Load input to stack
                gen.Emit(OpCodes.Castclass, sourceType); // Cast to source type
                gen.Emit(OpCodes.Ldarg_1); // Load value to stack
                gen.Emit(OpCodes.Unbox_Any, field.FieldType); // Unbox the value to its proper value type
                gen.Emit(OpCodes.Stfld, field); // Set the value to the input field
                gen.Emit(OpCodes.Ret);

                callback = (Action<object, object>) method.CreateDelegate(typeof(Action<object, object>));
                FieldInfoToLateBoundFieldSet[field] = callback;
            }

            return callback;
        }

Same methods

DelegateFactory::CreateSet ( PropertyInfo property ) : object>.Action

Usage Example

Exemplo n.º 1
0
        object GetObjectOfTypeFromNode(Type t, XmlNode node)
        {
            if (t.IsSimpleType() || t == typeof(Uri) || t.IsNullableType())
            {
                return(GetPropertyValue(t, node));
            }

            if (typeof(IEnumerable).IsAssignableFrom(t))
            {
                return(GetPropertyValue(t, node));
            }

            var result = mapper.CreateInstance(t);

            foreach (XmlNode n in node.ChildNodes)
            {
                Type type = null;
                if (n.Name.Contains(":"))
                {
                    type = Type.GetType($"System.{n.Name.Substring(0, n.Name.IndexOf(":"))}", false, true);
                }

                if (!cache.typeMembers.TryGetValue(t, out var typeMembers))
                {
                    cache.InitType(t);
                    cache.typeMembers.TryGetValue(t, out typeMembers);
                }

                var prop = GetProperty(typeMembers?.Item2, n.Name);
                if (prop != null)
                {
                    var val = GetPropertyValue(type ?? prop.PropertyType, n);
                    if (val != null)
                    {
                        var propertySet = DelegateFactory.CreateSet(prop);
                        propertySet.Invoke(result, val);
                        continue;
                    }
                }

                var field = GetField(typeMembers?.Item1, n.Name);
                if (field != null)
                {
                    var val = GetPropertyValue(type ?? field.FieldType, n);
                    if (val != null)
                    {
                        var fieldSet = DelegateFactory.CreateSet(field);
                        fieldSet.Invoke(result, val);
                    }
                }
            }

            return(result);
        }
All Usage Examples Of NServiceBus.DelegateFactory::CreateSet
DelegateFactory