コード例 #1
0
        public void TheConvertMethod(Type type, string memberName, string expectedValue)
        {
            var actualValue = string.Empty;

            if (string.IsNullOrWhiteSpace(memberName))
            {
                actualValue = ConvertValue(type);
                Assert.AreEqual(expectedValue, actualValue);
                return;
            }

            var propertyInfo = type.GetPropertyEx(memberName);
            if (propertyInfo != null)
            {
                actualValue = ConvertValue(propertyInfo);
                Assert.AreEqual(expectedValue, actualValue);
                return;
            }

            var fieldInfo = type.GetFieldEx(memberName);
            if (fieldInfo != null)
            {
                actualValue = ConvertValue(fieldInfo);
                Assert.AreEqual(expectedValue, actualValue);
                return;
            }

            // This must be an enum
            var enumValue = Enum.Parse(type, memberName);
            actualValue = ConvertValue(enumValue);
            Assert.AreEqual(expectedValue, actualValue);
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationModelInfo"/> class.
        /// </summary>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="catelProperties">The catel properties.</param>
        /// <param name="fields">The fields.</param>
        /// <param name="regularProperties">The properties.</param>
        public SerializationModelInfo(Type modelType, Dictionary<string, MemberMetadata> catelProperties, Dictionary<string, MemberMetadata> fields,
            Dictionary<string, MemberMetadata> regularProperties)
        {
            Argument.IsNotNull("modelType", modelType);
            Argument.IsNotNull("catelProperties", catelProperties);
            Argument.IsNotNull("fields", fields);
            Argument.IsNotNull("properties", regularProperties);

            ModelType = modelType;

            CatelTypeInfo catelTypeInfo = null;

            CatelPropertyNames = new HashSet<string>(catelProperties.Keys);
            CatelProperties = new List<PropertyData>();
            CatelPropertiesByName = catelProperties;
            foreach (var catelProperty in catelProperties)
            {
                var propertyData = catelProperty.Value.Tag as PropertyData;
                if (propertyData == null)
                {
                    if (catelTypeInfo == null)
                    {
                        catelTypeInfo = PropertyDataManager.Default.GetCatelTypeInfo(modelType);
                    }

                    propertyData = catelTypeInfo.GetPropertyData(catelProperty.Key);
                }

                CatelProperties.Add(propertyData);
            }

            FieldNames = new HashSet<string>(fields.Keys);
            Fields = new List<FieldInfo>();
            FieldsByName = fields;
            foreach (var field in fields)
            {
                var fieldInfo = field.Value.Tag as FieldInfo;
                if (fieldInfo == null)
                {
                    fieldInfo = modelType.GetFieldEx(field.Key);
                }

                Fields.Add(fieldInfo);
            }

            PropertyNames = new HashSet<string>(regularProperties.Keys);
            Properties = new List<PropertyInfo>();
            PropertiesByName = regularProperties;
            foreach (var regularProperty in regularProperties)
            {
                var propertyInfo = regularProperty.Value.Tag as PropertyInfo;
                if (propertyInfo == null)
                {
                    propertyInfo = modelType.GetPropertyEx(regularProperty.Key);
                }

                Properties.Add(propertyInfo);
            }
        }
コード例 #3
0
        /// <summary>
        /// Creates a command using a naming convention with the specified gesture.
        /// </summary>
        /// <param name="commandManager">The command manager.</param>
        /// <param name="containerType">Type of the container.</param>
        /// <param name="commandNameFieldName">Name of the command name field.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="commandManager"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="containerType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="commandNameFieldName"/> is <c>null</c>.</exception>
        public static void CreateCommandWithGesture(this ICommandManager commandManager, Type containerType, string commandNameFieldName)
        {
            Argument.IsNotNull("commandManager", commandManager);
            Argument.IsNotNull("containerType", containerType);
            Argument.IsNotNullOrWhitespace("commandNameFieldName", commandNameFieldName);

            Log.Debug("Creating command '{0}'", commandNameFieldName);

            // Note: we must store bindingflags inside variable otherwise invalid IL will be generated
            var bindingFlags = BindingFlags.Public | BindingFlags.Static;
            var commandNameField = containerType.GetFieldEx(commandNameFieldName, bindingFlags);
            if (commandNameField == null)
            {
                throw Log.ErrorAndCreateException<InvalidOperationException>("Command '{0}' is not available on container type '{1}'",
                    commandNameFieldName, containerType.GetSafeFullName());
            }

            var commandName = (string)commandNameField.GetValue(null);
            if (commandManager.IsCommandCreated(commandName))
            {
                Log.Debug("Command '{0}' is already created, skipping...", commandName);
                return;
            }

            InputGesture commandInputGesture = null;
            var inputGestureField = containerType.GetFieldEx(string.Format("{0}InputGesture", commandNameFieldName), bindingFlags);
            if (inputGestureField != null)
            {
                commandInputGesture = inputGestureField.GetValue(null) as InputGesture;
            }

            commandManager.CreateCommand(commandName, commandInputGesture);

            var commandContainerName = string.Format("{0}CommandContainer", commandName.Replace(".", string.Empty));

            var commandContainerType = (from type in TypeCache.GetTypes()
                                        where string.Equals(type.Name, commandContainerName, StringComparison.OrdinalIgnoreCase)
                                        select type).FirstOrDefault();
            if (commandContainerType != null)
            {
                Log.Debug("Found command container '{0}', registering it in the ServiceLocator now", commandContainerType.GetSafeFullName());

                var serviceLocator = commandManager.GetServiceLocator();
                if (!serviceLocator.IsTypeRegistered(commandContainerType))
                {
                    var typeFactory = serviceLocator.ResolveType<ITypeFactory>();
                    var commandContainer = typeFactory.CreateInstance(commandContainerType);
                    if (commandContainer != null)
                    {
                        serviceLocator.RegisterInstance(commandContainer);
                    }
                    else
                    {
                        Log.Warning("Cannot create command container '{0}', skipping registration", commandContainerType.GetSafeFullName());
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationModelInfo"/> class.
        /// </summary>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="catelPropertyNames">The catel property names.</param>
        /// <param name="fieldNames">The fields.</param>
        /// <param name="propertyNames">The properties.</param>
        public SerializationModelInfo(Type modelType, HashSet<string> catelPropertyNames, HashSet<string> fieldNames, HashSet<string> propertyNames)
        {
            Argument.IsNotNull("modelType", modelType);
            Argument.IsNotNull("catelPropertyNames", catelPropertyNames);
            Argument.IsNotNull("fields", fieldNames);
            Argument.IsNotNull("properties", propertyNames);

            ModelType = modelType;

            var catelTypeInfo = PropertyDataManager.Default.GetCatelTypeInfo(modelType);

            CatelPropertyNames = catelPropertyNames;
            CatelProperties = new List<PropertyData>();
            CatelPropertiesByName = new Dictionary<string, PropertyData>();
            foreach (var catelPropertyName in catelPropertyNames)
            {
                var propertyData = catelTypeInfo.GetPropertyData(catelPropertyName);

                CatelProperties.Add(propertyData);
                CatelPropertiesByName[propertyData.Name] = propertyData;
            }

            FieldNames = fieldNames;
            Fields = new List<FieldInfo>();
            FieldsByName = new Dictionary<string, FieldInfo>();
            foreach (var fieldName in fieldNames)
            {
                var fieldInfo = modelType.GetFieldEx(fieldName);

                Fields.Add(fieldInfo);
                FieldsByName[fieldName] = fieldInfo;
            }

            PropertyNames = propertyNames;
            Properties = new List<PropertyInfo>();
            PropertiesByName = new Dictionary<string, PropertyInfo>();
            foreach (var propertyName in propertyNames)
            {
                var propertyInfo = modelType.GetPropertyEx(propertyName);

                Properties.Add(propertyInfo);
                PropertiesByName[propertyName] = propertyInfo;
            }
        }
コード例 #5
0
        private static DependencyProperty GetDependencyProperty(Type type, string name)
        {
#if WPF
            DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromName(name, type, type);
            if (descriptor == null)
                return null;
            return descriptor.DependencyProperty;
#else

            FieldInfo fieldInfo = type.GetFieldEx(name + "Property", MemberFlags.Public | MemberFlags.Static) ??
                                  type.GetFieldEx(name, MemberFlags.Public | MemberFlags.Static);
            var property = fieldInfo == null
                ? null
                : fieldInfo.GetValue(null) as DependencyProperty;
            if (property == null)
            {
                var prop = type.GetPropertyEx(name + "Property", MemberFlags.Public | MemberFlags.Static) ??
                                  type.GetPropertyEx(name, MemberFlags.Public | MemberFlags.Static);
                property = (prop == null || !prop.CanRead)
                    ? null
                    : prop.GetValue(null, null) as DependencyProperty;
            }
            return property;
#endif
        }
コード例 #6
0
        /// <summary>
        /// Gets the fields
        /// </summary>
        /// <param name="type">Type of the model.</param>
        /// <returns>A hash set containing the fields.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="type"/> is <c>null</c>.</exception>
        public Dictionary<string, MemberMetadata> GetFields(Type type)
        {
            Argument.IsNotNull("type", type);

            return _fieldsCache.GetFromCacheOrFetch(type, () =>
            {
                var dictionary = new Dictionary<string, MemberMetadata>();

                var fieldNames = GetFieldNames(type);
                foreach (var field in fieldNames)
                {
                    var fieldInfo = type.GetFieldEx(field);
                    if (fieldInfo != null)
                    {
                        dictionary[field] = new MemberMetadata(type, fieldInfo.FieldType, SerializationMemberGroup.Field, field);
                    }
                }

                return dictionary;
            });
        }
コード例 #7
0
ファイル: XmlSchemaHelper.cs プロジェクト: JaysonJG/Catel
        /// <summary>
        /// Returns the sequence of properties of the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="schema">The schema.</param>
        /// <param name="schemaSet">The schema set.</param>
        /// <param name="serializationManager">The serialization manager.</param>
        /// <returns>Sequence containing all properties.</returns>
        private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager)
        {
            Argument.IsNotNull("type", type);
            Argument.IsNotNull("schema", schema);
            Argument.IsNotNull("schemaSet", schemaSet);

            var propertiesSequence = new XmlSchemaSequence();

            if (typeof(ModelBase).IsAssignableFromEx(type))
            {
                var typeNs = GetTypeNamespaceForSchema(type);

                var members = new List<MemberInfo>();
                members.AddRange(from field in serializationManager.GetFieldsToSerialize(type)
                                 select type.GetFieldEx(field));
                members.AddRange(from property in serializationManager.GetPropertiesToSerialize(type)
                                 select type.GetPropertyEx(property));

                foreach (var member in members)
                {
                    var propertySchemaElement = new XmlSchemaElement();
                    propertySchemaElement.Name = member.Name;

                    var memberType = typeof(object);
                    var fieldInfo = member as FieldInfo;
                    if (fieldInfo != null)
                    {
                        memberType = fieldInfo.FieldType;
                    }

                    var propertyInfo = member as PropertyInfo;
                    if (propertyInfo != null)
                    {
                        memberType = propertyInfo.PropertyType;
                    }

                    if (memberType.ImplementsInterfaceEx(typeof(IEnumerable)) && memberType != typeof(string))
                    {
                        propertySchemaElement.SchemaTypeName = new XmlQualifiedName(string.Format("{0}", member.Name), typeNs);

                        var collectionPropertyType = new XmlSchemaComplexType();
                        collectionPropertyType.Name = string.Format("{0}", member.Name);
                        schema.Items.Add(collectionPropertyType);

                        foreach (var genericArgument in memberType.GetGenericArguments())
                        {
                            AddTypeToSchemaSet(genericArgument, schemaSet, serializationManager);
                        }
                    }
                    else
                    {
                        propertySchemaElement.SchemaTypeName = AddTypeToSchemaSet(memberType, schemaSet, serializationManager);
                        propertySchemaElement.IsNillable = TypeHelper.IsTypeNullable(memberType);
                        propertySchemaElement.MinOccurs = 0;
                    }

                    propertiesSequence.Items.Add(propertySchemaElement);
                }
            }

            return propertiesSequence;
        }
コード例 #8
0
        private static MemberInfo TryFindMetaMemberInfo(Type metaType, MemberInfo member)
        {
            var flags = MemberFlags.Public | MemberFlags.NonPublic | MemberFlags.Instance;
            var property = member as PropertyInfo;
            if (property != null)
                return metaType
                    .GetPropertiesEx(flags)
                    .FirstOrDefault(
                        info =>
                            info.Name == property.Name &&
                            info.GetIndexParameters().SequenceEqual(property.GetIndexParameters()));
            var field = member as FieldInfo;
            if (field != null)
                return metaType.GetFieldEx(field.Name, flags);
            var method = member as MethodInfo;
            if (method != null)
                return metaType
                    .GetMethodsEx(flags)
                    .FirstOrDefault(info => info.Name == method.Name && info.ReturnType.Equals(method.ReturnType)
                                            &&
                                            info.GetParameters()
                                                .Select(parameterInfo => parameterInfo.ParameterType)
                                                .SequenceEqual(method.GetParameters().Select(parameterInfo => parameterInfo.ParameterType)));
            var eventInfo = member as EventInfo;
            if (eventInfo != null)
#if PCL_WINRT
                return metaType.GetRuntimeEvents().FirstOrDefault(info => info.Name == eventInfo.Name);
#else
                return metaType.GetEvents().FirstOrDefault(info => info.Name == eventInfo.Name);
#endif

            return null;
        }
コード例 #9
0
ファイル: XmlSchemaHelper.cs プロジェクト: justdude/DbExport
        /// <summary>
        /// Returns the sequence of properties of the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="schema">The schema.</param>
        /// <param name="schemaSet">The schema set.</param>
        /// <param name="serializationManager">The serialization manager.</param>
        /// <returns>Sequence containing all properties.</returns>
        private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager)
        {
            Argument.IsNotNull("type", type);
            Argument.IsNotNull("schema", schema);
            Argument.IsNotNull("schemaSet", schemaSet);

            var propertiesSequence = new XmlSchemaSequence();

            if (typeof(ModelBase).IsAssignableFromEx(type))
            {
                var members = new List<MemberInfo>();
                members.AddRange(from field in serializationManager.GetFieldsToSerialize(type)
                                 select type.GetFieldEx(field));
                members.AddRange(from property in serializationManager.GetPropertiesToSerialize(type)
                                 select type.GetPropertyEx(property));

                foreach (var member in members)
                {
                    var propertySchemaElement = new XmlSchemaElement();
                    propertySchemaElement.Name = member.Name;

                    var memberType = typeof(object);
                    var fieldInfo = member as FieldInfo;
                    if (fieldInfo != null)
                    {
                        memberType = fieldInfo.FieldType;
                    }

                    var propertyInfo = member as PropertyInfo;
                    if (propertyInfo != null)
                    {
                        memberType = propertyInfo.PropertyType;
                    }

                    propertySchemaElement.IsNillable = memberType.IsNullableType();
                    propertySchemaElement.MinOccurs = 0;

                    var exporter = new XsdDataContractExporter(schemaSet);
                    exporter.Export(memberType);

                    propertySchemaElement.SchemaType = exporter.GetSchemaType(memberType);
                    propertySchemaElement.SchemaTypeName = exporter.GetSchemaTypeName(memberType);

                    propertiesSequence.Items.Add(propertySchemaElement);
                }
            }

            return propertiesSequence;
        }