Beispiel #1
0
        /// <summary>
        /// Return a writer for writing a single property value.
        /// </summary>
        /// <param name="propertyName">to write to</param>
        /// <returns>
        /// null or writer if writable
        /// </returns>
        public EventPropertyWriter GetWriter(String propertyName)
        {
            if (_writeablePropertyDescriptors == null)
            {
                InitializeWriters();
            }

            var pair = _writerMap.Get(propertyName);

            if (pair != null)
            {
                return(pair.Second);
            }

            var property = PropertyParser.ParseAndWalk(propertyName, false);

            if (property is MappedProperty)
            {
                var mapProp    = (MappedProperty)property;
                var methodName = string.Format("Set{0}", mapProp.PropertyNameAtomic);
                var methodInfo = UnderlyingType.GetMethod(
                    methodName, BindingFlags.Public | BindingFlags.Instance, null,
                    new Type[] { typeof(string), typeof(object) }, null);
                if (methodInfo == null)
                {
                    Log.Info("Failed to find mapped property '" + mapProp.PropertyNameAtomic +
                             "' for writing to property '" + propertyName + "'");
                    return(null);
                }

                var fastMethod = FastClass.GetMethod(methodInfo);
                return(new BeanEventPropertyWriterMapProp(UnderlyingType, fastMethod, mapProp.Key));
            }

            if (property is IndexedProperty)
            {
                var indexedProp = (IndexedProperty)property;
                var methodName  = string.Format("Set{0}", indexedProp.PropertyNameAtomic);
                var methodInfo  = UnderlyingType.GetMethod(
                    methodName, BindingFlags.Public | BindingFlags.Instance, null,
                    new Type[] { typeof(int), typeof(object) }, null);
                if (methodInfo == null)
                {
                    Log.Info("Failed to find mapped property '" + indexedProp.PropertyNameAtomic +
                             "' for writing to property '" + propertyName + "'");
                    return(null);
                }

                var fastMethod = FastClass.GetMethod(methodInfo);
                return(new BeanEventPropertyWriterIndexedProp(UnderlyingType, fastMethod, indexedProp.Index));
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExtendedTypeInfo"/> class.
        /// </summary>
        /// <param name="t">The t.</param>
        public ExtendedTypeInfo(Type t)
        {
            Type = t ?? throw new ArgumentNullException(nameof(t));
            IsNullableValueType = Type.GetTypeInfo().IsGenericType &&
                                  Type.GetGenericTypeDefinition() == typeof(Nullable <>);

            IsValueType = t.GetTypeInfo().IsValueType;

            UnderlyingType = IsNullableValueType ?
                             new NullableConverter(Type).UnderlyingType :
                             Type;

            IsNumeric = NumericTypes.Contains(UnderlyingType);

            // Extract the TryParse method info
            try
            {
                TryParseMethodInfo = UnderlyingType.GetMethod(TryParseMethodName,
                                                              new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), UnderlyingType.MakeByRefType() }) ??
                                     UnderlyingType.GetMethod(TryParseMethodName,
                                                              new[] { typeof(string), UnderlyingType.MakeByRefType() });

                _tryParseParameters = TryParseMethodInfo?.GetParameters();
            }
            catch
            {
                // ignored
            }

            // Extract the ToString method Info
            try
            {
                ToStringMethodInfo = UnderlyingType.GetMethod(ToStringMethodName,
                                                              new[] { typeof(IFormatProvider) }) ??
                                     UnderlyingType.GetMethod(ToStringMethodName,
                                                              new Type[] { });

                _toStringArgumentLength = ToStringMethodInfo?.GetParameters().Length ?? 0;
            }
            catch
            {
                // ignored
            }
        }
Beispiel #3
0
        public EventBeanCopyMethod GetCopyMethod(String[] properties)
        {
            if (_copyMethodName == null)
            {
                if (UnderlyingType.IsSerializable)
                {
                    return(new BeanEventBeanSerializableCopyMethod(this, _eventAdapterService));
                }

                if (UnderlyingType.IsInterface)
                {
                    return(new BeanEventBeanSerializableCopyMethod(this, _eventAdapterService));
                }

                return(null);
            }
            MethodInfo method = null;

            try
            {
                method = UnderlyingType.GetMethod(_copyMethodName);
            }
            catch (AmbiguousMatchException e)
            {
                Log.Error("Configured copy-method for class '" + UnderlyingType.Name + " not found by name '" + _copyMethodName + "': " + e.Message);
            }

            if (method == null)
            {
                if (UnderlyingType.IsSerializable)
                {
                    return(new BeanEventBeanSerializableCopyMethod(this, _eventAdapterService));
                }

                throw new EPException("Configured copy-method for class '" + UnderlyingType.Name + " not found by name '" + _copyMethodName + "' and type is not Serializable");
            }

            return(new BeanEventBeanConfiguredCopyMethod(this, _eventAdapterService, FastClass.GetMethod(method)));
        }