Example #1
0
        static EntityViewInfo BuildViewFromReflection(TypeDefinition typedefinition)
        {
            var viewInfo = new EntityViewInfo()
            {
                EntityName              = typedefinition.FullName,
                LabelResourceName       = PrintHelper.PrintClassResourceName(typedefinition.FullName, ResourceItemType.Label),
                DescriptionResourceName = PrintHelper.PrintClassResourceName(typedefinition.FullName, ResourceItemType.Description)
            };

            foreach (var customAttr in typedefinition.CustomAttributes)
            {
                Console.WriteLine(customAttr.AttributeType);
                Console.WriteLine(customAttr.Constructor.DeclaringType);
            }

            foreach (var propDef in typedefinition.Properties)
            {
                PropertyViewInfo pview        = new PropertyViewInfo();
                string           groupName    = "default";
                string           resourceType = string.Empty;
                pview.Name = propDef.Name;
                bool attributeFound = false;
                foreach (var attr in propDef.CustomAttributes)
                {
                    if (attr.AttributeType.Name.Equals("DisplayAttribute"))
                    {
                        attributeFound = true;
                        foreach (var attProp in attr.Properties)
                        {
                            if (attProp.Name.Equals("Name") && (attProp.Argument.Value != null))
                            {
                                pview.LabelResourceName = attProp.Argument.Value.ToString();
                                continue;
                            }

                            if (attProp.Name.Equals("Description") && (attProp.Argument.Value != null))
                            {
                                pview.DescriptionResourceName = attProp.Argument.Value.ToString();
                                continue;
                            }

                            if (attProp.Name.Equals("ResourceType") && (attProp.Argument.Value != null))
                            {
                                pview.ResourceType = attProp.Argument.Value.ToString();
                                resourceType       = pview.ResourceType;
                                continue;
                            }

                            if (attProp.Name.Equals("Prompt") && (attProp.Argument.Value != null))
                            {
                                pview.PromptResourceName = attProp.Argument.Value.ToString();
                                continue;
                            }

                            if (attProp.Name.Equals("GroupName") && (attProp.Argument.Value != null))
                            {
                                groupName = attProp.Argument.Value.ToString();
                                continue;
                            }

                            if (attProp.Name.Equals("Order") && (attProp.Argument.Value != null))
                            {
                                var orderString = attProp.Argument.Value.ToString();
                                int order;
                                if (int.TryParse(orderString, out order))
                                {
                                    pview.Order = order;
                                }
                            }
                        }
                    }

                    if (attr.AttributeType.Name.Equals("EditableAttribute"))
                    {
                        if (attr.HasConstructorArguments && attr.ConstructorArguments[0].Value != null)
                        {
                            attributeFound = true;
                            var editableValue = attr.ConstructorArguments[0].Value.ToString();

                            bool isEditable;
                            if (bool.TryParse(editableValue, out isEditable))
                            {
                                pview.Editable = isEditable;
                            }
                            else
                            {
                                pview.Editable = true;
                            }
                        }
                    }

                    if (attr.AttributeType.Name.Equals("RequiredAttribute"))
                    {
                        foreach (var attProp in attr.Properties)
                        {
                            if (attProp.Name.Equals("ErrorMessageResourceName") && (attProp.Argument.Value != null))
                            {
                                attributeFound = true;
                                pview.RequiredErrorResourceName = attProp.Argument.Value.ToString();
                            }
                        }
                    }
                }

                if (!attributeFound)
                {
                    continue;
                }

                PropertyGroupViewInfo propCollection;
                if (!viewInfo.TryGetValue(groupName, out propCollection))
                {
                    string groupLabel;
                    if ("default".Equals(groupName))
                    {
                        groupLabel = PrintHelper.PrintPropertyGroupResourceName(groupName, ResourceItemType.Label);
                    }

                    else
                    {
                        groupLabel = groupName;
                    }

                    propCollection = new PropertyGroupViewInfo()
                    {
                        Name = groupName,
                        LabelResourceName       = groupLabel,
                        ResourceType            = resourceType,
                        DescriptionResourceName = PrintHelper.PrintPropertyGroupResourceName(groupName, ResourceItemType.Description)
                    };
                    viewInfo.Add(propCollection);
                }

                propCollection.Add(pview);
            }

            return(viewInfo);
        }