NServiceBus.DelegateFactory.CreateGet C# (CSharp) Method

CreateGet() public static method

public static CreateGet ( FieldInfo field ) : object>.Func
field System.Reflection.FieldInfo
return object>.Func
        public static Func<object, object> CreateGet(FieldInfo field)
        {
            Func<object, object> lateBoundFieldGet;

            if (!FieldInfoToLateBoundField.TryGetValue(field, out lateBoundFieldGet))
            {
                var instanceParameter = Expression.Parameter(typeof(object), "target");

                var member = Expression.Field(Expression.Convert(instanceParameter, field.DeclaringType), field);

                var lambda = Expression.Lambda<Func<object, object>>(
                    Expression.Convert(member, typeof(object)),
                    instanceParameter
                    );

                lateBoundFieldGet = lambda.Compile();
                FieldInfoToLateBoundField[field] = lateBoundFieldGet;
            }

            return lateBoundFieldGet;
        }

Same methods

DelegateFactory::CreateGet ( PropertyInfo property ) : object>.Func

Usage Example

        void Write(XElement elem, Type t, object obj)
        {
            if (obj == null)
            {
                // For null entries in a nullable array
                // See https://github.com/Particular/NServiceBus/issues/2706
                if (t.IsNullableType())
                {
                    elem.Value = "null";
                }

                return;
            }

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

            foreach (var prop in members.Item2)
            {
                if (IsIndexedProperty(prop))
                {
                    throw new NotSupportedException($"Type {t.FullName} contains an indexed property named {prop.Name}. Indexed properties are not supported on message types.");
                }
                WriteEntry(elem, prop.Name, prop.PropertyType, DelegateFactory.CreateGet(prop).Invoke(obj));
            }

            foreach (var field in members.Item1)
            {
                WriteEntry(elem, field.Name, field.FieldType, DelegateFactory.CreateGet(field).Invoke(obj));
            }
        }
All Usage Examples Of NServiceBus.DelegateFactory::CreateGet
DelegateFactory