未使用MetadataType加载元数据

时间:2013-07-26 05:26:57

标签: c# .net linq-to-sql

我对MetadataType有一些问题/疑问。我有使用LinqToSQL从MS SQL Server进行数据访问的DLL帮助项目。我还需要为生成的类ClientInfoView添加元数据。我按照以下方式做到了:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace DataAPI.LINQToSQL
{
    [MetadataType(typeof(ClientInfoViewMetaData))]
    public partial class ClientInfoView
    {
        internal sealed class ClientInfoViewMetaData
        {
            [Category("Main Data"), DisplayName("Client ID")]
            public int ID { get; set; }

            [Category("Main Data"), DisplayName("Login")]
            public string Login { get; set; }

            ...
        }
    }
}

但是当我在运行时检查属性时,我发现ClientInfoView没有任何属性。

你能帮我找个错误吗?

3 个答案:

答案 0 :(得分:2)

因为metadatatype不适用于此类方法,但您可以使用此方法

private bool PropertyHasAttribute<T>(string properyName, Type attributeType)
    {
        MetadataTypeAttribute att = (MetadataTypeAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(MetadataTypeAttribute));
        if (att != null)
        {
            ;
            foreach (var prop in Type.GetType(att.MetadataClassType.UnderlyingSystemType.FullName).GetProperties())
            {
                if (properyName.ToLower() == properyName.ToLower() && Attribute.IsDefined(prop,attributeType))
                    return true;
            }
        }
        return false;
    }

你可以像这样使用

bool res = PropertyHasAttribute<ClientInfoView>("Login", typeof(DisplayAttribute))

这告诉你类属性登录有或没有displayattribute,但是如果你需要findout属性值,你可以使用Attribute.GetCustomAttribute方法并将它转换为你所选择的属性,如display属性和读取Name属性by .Name: )

答案 1 :(得分:1)

要给出部分答案,您可以检查ClientInfoView是否有属性。一些适合我的小型演示。仍然试图找到我无法在ClientInfoViewMetaData个别属性中访问这些属性的原因

    static void Main(string[] args)
    {
        TypeDescriptor.AddProviderTransparent(
        new AssociatedMetadataTypeTypeDescriptionProvider(typeof(ClientInfoView), typeof(ClientInfoViewMetaData)), typeof(ClientInfoView));
        ClientInfoView cv1 = new ClientInfoView() { ID = 1 };
        var df = cv1.GetType().GetCustomAttributes(true);
        var dfd = cv1.ID.GetType().GetCustomAttributes(typeof(DisplayNameAttribute), true);
        var context = new ValidationContext(cv1, null, null);
        var results = new List<ValidationResult>();
        var isValid = Validator.TryValidateObject( cv1,context, results, true);
    }
}

    [MetadataType(typeof(ClientInfoViewMetaData))]
    public partial class ClientInfoView
    {
        public int ID { get; set; }
        public string Login { get; set; }
    }

public class ClientInfoViewMetaData
{        
    [Required]
    [Category("Main Data"), DisplayName("Client ID")]
    public int ID { get; set; }

    [Required]
    [Category("Main Data"), DisplayName("Login")]
    public string Login { get; set; }

}

答案 2 :(得分:0)

或者你可以使用这个基于elia07的扩展方法回答:

 Dim md As ModelMetadata = ...
 Dim isReadOnly As Boolean = md.HasAttribute(Of Cikkek, ReadOnlyFW)

样本:

@using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <p>Welcome!</p>
            <legend><h2>Log in</h2></legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.UserName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.UserName)
                @Html.ValidationMessageFor(model => model.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Password)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.RememberMe)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.RememberMe)
            </div>    
            <p>
                <input type="submit" value="Enter"/>
            </p>
        </fieldset>
    }