Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SharedValueMemberInfo"/>.
        /// </summary>
        /// <param name="reflectionManager">The <see cref="SharedReflectionManager"/> used for the reflection.</param>
        /// <param name="declaringType">The <see cref="SharedTypeInfo"/> of the declaring type.</param>
        /// <param name="member">The member which is represented.</param>
        /// <param name="valueType">The type of the value hold by the member.</param>
        /// <param name="canWrite">Specifies if the member can be written.</param>
        internal SharedValueMemberInfo(SharedReflectionManager reflectionManager, SharedTypeInfo declaringType, MemberInfo member, Type valueType, Boolean canWrite, Object defaultValue)
            : base(declaringType, member)
        {
            if (reflectionManager == null)
            {
                throw new ArgumentNullException(nameof(reflectionManager));
            }
            if (valueType == null)
            {
                throw new ArgumentNullException(nameof(valueType));
            }

            CanWrite      = canWrite;
            ValueType     = reflectionManager.GetInfo(valueType.GetTypeInfo());
            _DefaultValue = ((member.GetCustomAttribute <DefaultValueAttribute>() is DefaultValueAttribute defaultAttribute) ? defaultAttribute.Value : defaultValue) ??
                            (ValueType.IsValueType ? Activator.CreateInstance(valueType) : null);
        }
Exemplo n.º 2
0
        internal SharedTypeInfo(SharedReflectionManager reflectionManager, SharedReflectionManager.RegisterAction registerCallback, TypeInfo type)
        {
            if (reflectionManager == null)
            {
                throw new ArgumentNullException(nameof(reflectionManager));
            }
            if (registerCallback == null)
            {
                throw new ArgumentNullException(nameof(registerCallback));
            }

            // Basic initialization
            Type     = type ?? throw new ArgumentNullException(nameof(type));
            IsStatic = type.IsSealed && type.IsAbstract;

            // Generate a sample object if possible
            if (!type.IsAbstract)
            {
                try
                {
                    // Search for a matching constructor
                    if (type.DeclaredConstructors.FirstOrDefault(p => !p.IsStatic && p.IsPublic && p.GetParameters().Length == 0) is ConstructorInfo constructor)
                    {
                        // Generate a default value
                        DefaultValue = constructor.Invoke(null);
                    }
                }
                // Catch all exceptions
                catch { }
            }


            // Registers the current type in the manager
            registerCallback(this);

            // Get the basetype of the current type
            if (Type.BaseType != null)
            {
                BaseType = reflectionManager.GetInfo(Type.BaseType.GetTypeInfo());
            }

            // Start a new task which initializes the type
            Task.Factory.StartNew(() => InitializeType(reflectionManager), TaskCreationOptions.LongRunning);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes the current type.
        /// </summary>
        void InitializeType(SharedReflectionManager reflectionManager)
        {
            using (_ObjectParseCompletion)
            {
                List <SharedPropertyInfo> properties = new List <SharedPropertyInfo>();
                Dictionary <String, DependencyProperty> propertyLookup = DependencyType.GetDependencyType(Type, false).Properties.ToDictionary(p => p.Name);

                // Go through all declared properties and generate the property infos
                foreach (PropertyInfo item in Type.DeclaredProperties)
                {
                    propertyLookup.TryGetValue(item.Name, out DependencyProperty dependencyProperty);
                    properties.Add(new SharedPropertyInfo(reflectionManager, this, item, dependencyProperty, DefaultValue));
                }
                // Assign properties to the object
                _DeclaredProperties       = properties.ToImmutableArray();
                _DeclaredPropertiesLookup = _DeclaredProperties.ToImmutableDictionary(p => p.Name);

                // Set parsing to finished
                _ObjectParseCompletion.Set();
            }
            _ObjectParseCompletion = null;
        }
Exemplo n.º 4
0
 public SharedPropertyInfo(SharedReflectionManager reflectionManager, SharedTypeInfo declaringType, PropertyInfo property, DependencyProperty dependencyProperty, Object defaultValueSample)
     : base(reflectionManager, declaringType, property, dependencyProperty?.PropertyType.AsType() ?? property.PropertyType, CanWriteProperty(property, dependencyProperty), SampleDefault(property, dependencyProperty, defaultValueSample))
 {
     DependencyProperty = dependencyProperty;
     _Property          = property;
 }