Exemplo n.º 1
0
        private string GetStringForEnumOrDefinedType(MProperty property)
        {
            var value = string.Empty;

            if (property.IsEnum)
            {
                value = " This is a hard coded list of values defined in the code as an enumeration.";
            }
            else if (property.DefinedTypeId.HasValue)
            {
                var definedType = DefinedTypeCache.Get(property.DefinedTypeId.Value);
                value = $" These are found in the \"<a href=\"{DEFINED_TYPE_ROUTE}{definedType.Id}\">{definedType.Name}</a>\" Defined Type.";
            }

            return(value);
        }
Exemplo n.º 2
0
        private void ShowData(Guid?categoryGuid, int?entityTypeId)
        {
            if (EntityCategories == null)
            {
                LoadCategories();
            }

            hfSelectedCategoryGuid.Value = categoryGuid.ToString();
            hfSelectedEntityId.Value     = null;

            // Bind Categories
            rptCategory.DataSource = EntityCategories;
            rptCategory.DataBind();

            pnlModels.Visible  = false;
            pnlKey.Visible     = false;
            lCategoryName.Text = string.Empty;

            EntityTypeCache entityType     = null;
            var             entityTypeList = new List <EntityTypeCache>();

            if (categoryGuid.HasValue)
            {
                var category = EntityCategories.Where(c => c.Guid.Equals(categoryGuid)).FirstOrDefault();
                if (category != null)
                {
                    lCategoryName.Text = category.Name.SplitCase() + " Models";
                    pnlModels.Visible  = true;

                    entityTypeList = category
                                     .RockEntityIds
                                     .Select(a => EntityTypeCache.Get(a))
                                     .Where(a => a != null)
                                     .OrderBy(et => et.FriendlyName)
                                     .ToList();
                    if (entityTypeId.HasValue)
                    {
                        entityType = entityTypeList.Where(t => t.Id == entityTypeId.Value).FirstOrDefault();
                        hfSelectedEntityId.Value = entityType != null?entityType.Id.ToString() : null;
                    }
                    else
                    {
                        entityType = entityTypeList.FirstOrDefault();
                        hfSelectedEntityId.Value = entityTypeList.Any() ? entityTypeList.First().Id.ToString() : null;
                    }
                }
            }

            // Bind Models
            rptModel.DataSource = entityTypeList;
            rptModel.DataBind();

            string details = string.Empty;

            nbClassesWarning.Visible = false;
            pnlClassDetail.Visible   = false;
            if (entityType != null)
            {
                try
                {
                    var type = entityType.GetEntityType();
                    if (type != null)
                    {
                        pnlKey.Visible = true;

                        var xmlComments = GetXmlComments();

                        var mClass = new MClass();
                        mClass.Name    = type.Name;
                        mClass.Comment = GetComments(type, xmlComments);

                        PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                                    .Where(m => m.MemberType == MemberTypes.Method || m.MemberType == MemberTypes.Property)
                                                    .ToArray();
                        foreach (PropertyInfo p in properties.OrderBy(i => i.Name).ToArray())
                        {
#pragma warning disable CS0618 // LavaIncludeAttribute is obsolete
                            var property = new MProperty
                            {
                                Name            = p.Name,
                                IsInherited     = p.DeclaringType != type,
                                IsVirtual       = p.GetGetMethod() != null && p.GetGetMethod().IsVirtual&& !p.GetGetMethod().IsFinal,
                                IsLavaInclude   = p.IsDefined(typeof(LavaIncludeAttribute)) || p.IsDefined(typeof(LavaVisibleAttribute)) || p.IsDefined(typeof(DataMemberAttribute)),
                                IsObsolete      = p.IsDefined(typeof(ObsoleteAttribute)),
                                ObsoleteMessage = GetObsoleteMessage(p),
                                NotMapped       = p.IsDefined(typeof(NotMappedAttribute)),
                                Required        = p.IsDefined(typeof(RequiredAttribute)),
                                Id             = p.MetadataToken,
                                Comment        = GetComments(p, xmlComments, properties),
                                IsEnum         = p.PropertyType.IsEnum,
                                IsDefinedValue = p.Name.EndsWith("ValueId") && p.IsDefined(typeof(DefinedValueAttribute))
                            };
#pragma warning restore CS0618 // LavaIncludeAttribute is obsolete

                            if (property.IsEnum)
                            {
                                property.KeyValues = new Dictionary <string, string>();
                                var values = p.PropertyType.GetEnumValues();
                                foreach (var value in values)
                                {
                                    property.KeyValues.AddOrReplace((( int )value).ToString(), value.ToString());
                                }
                            }
                            else if (property.IsDefinedValue)
                            {
                                var definedValueAttribute = p.GetCustomAttribute <Rock.Data.DefinedValueAttribute>();
                                if (definedValueAttribute != null && definedValueAttribute.DefinedTypeGuid.HasValue)
                                {
                                    property.KeyValues = new Dictionary <string, string>();
                                    var definedTypeGuid = definedValueAttribute.DefinedTypeGuid.Value;
                                    var definedType     = DefinedTypeCache.Get(definedTypeGuid);
                                    property.DefinedTypeId = definedType.Id;
                                    foreach (var definedValue in definedType.DefinedValues)
                                    {
                                        property.KeyValues.AddOrReplace(string.Format("{0} = {1}", definedValue.Id, definedValue.Value), definedValue.Description);
                                    }
                                }
                            }

                            mClass.Properties.Add(property);
                        }

                        MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                                               .Where(m => !m.IsSpecialName && (m.MemberType == MemberTypes.Method || m.MemberType == MemberTypes.Property))
                                               .ToArray();
                        foreach (MethodInfo m in methods.OrderBy(i => i.Name).ToArray())
                        {
                            // crazy, right?
                            var param = string.Join(", ", m.GetParameters().Select(pi => { var x = pi.ParameterType + " " + pi.Name; return(x); }));

                            mClass.Methods.Add(new MMethod
                            {
                                Name            = m.Name,
                                IsInherited     = m.DeclaringType != type,
                                Id              = m.MetadataToken,
                                Signature       = string.Format("{0}({1})", m.Name, param),
                                Comment         = GetComments(m, xmlComments),
                                IsObsolete      = m.IsDefined(typeof(ObsoleteAttribute)),
                                ObsoleteMessage = GetObsoleteMessage(m)
                            });
                        }

                        var pageReference = new Rock.Web.PageReference(CurrentPageReference);
                        pageReference.QueryString = new System.Collections.Specialized.NameValueCollection();
                        pageReference.QueryString["EntityType"] = entityType.Guid.ToString();

                        lClassName.Text        = mClass.Name;
                        hlAnchor.NavigateUrl   = pageReference.BuildUrl();
                        lClassDescription.Text = mClass.Comment != null ? mClass.Comment.Summary : string.Empty;
                        lClasses.Text          = ClassNode(mClass);

                        pnlClassDetail.Visible = true;
                    }
                    else
                    {
                        nbClassesWarning.Text        = "Unable to get class details for " + entityType.FriendlyName;
                        nbClassesWarning.Details     = entityType.AssemblyName;
                        nbClassesWarning.Dismissable = true;
                        nbClassesWarning.Visible     = true;
                    }
                }
                catch (Exception ex)
                {
                    nbClassesWarning.Text        = string.Format("Error getting class details for <code>{0}</code>", entityType);
                    nbClassesWarning.Details     = ex.Message;
                    nbClassesWarning.Dismissable = true;
                    nbClassesWarning.Visible     = true;
                }
            }
        }