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);
        }