在mvc3服务器端代码中获取显示注释值

发布于 2024-11-29 06:22:19 字数 378 浏览 1 评论 0原文

有没有办法在服务器端代码中获取注释的值?例如,我有:

public class Dummy
{
    [Display(Name = "Foo")]
    public string foo { get; set; }

    [Display(Name = "Bar")]
    public string bar { get; set; }
}

我希望能够在服务器端获取值 "Foo" 而不将其发布回页面,但就像类的属性或类似的东西。就像 @Html.LabelFor(model => model.Foo) 但在 C# 服务器代码中。

这可能吗?

谢谢。

Is there a way to get the value of an annotation in server side code? For example, I have:

public class Dummy
{
    [Display(Name = "Foo")]
    public string foo { get; set; }

    [Display(Name = "Bar")]
    public string bar { get; set; }
}

I want to be able to get the value "Foo" on server side with out posting it back to the page, but like an attribute of the class, or something of the sort. Like a @Html.LabelFor(model => model.Foo) But in c# server code.

Is that possible?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-12-06 06:22:19

像这样的东西吗?

string displayName = GetDisplayName((Dummy x) => x.foo);

// ...

public static string GetDisplayName<T, U>(Expression<Func<T, U>> exp)
{
    var me = exp.Body as MemberExpression;
    if (me == null)
        throw new ArgumentException("Must be a MemberExpression.", "exp");

    var attr = me.Member
                 .GetCustomAttributes(typeof(DisplayAttribute), false)
                 .Cast<DisplayAttribute>()
                 .SingleOrDefault();

    return (attr != null) ? attr.Name : me.Member.Name;
}

或者,如果您希望能够针对实例调用该方法并利用类型推断:

var dummy = new Dummy();
string displayName = dummy.GetDisplayName(x => x.foo);

// ...

public static string GetDisplayName<T, U>(this T src, Expression<Func<T, U>> exp)
{
    var me = exp.Body as MemberExpression;
    if (me == null)
        throw new ArgumentException("Must be a MemberExpression.", "exp");

    var attr = me.Member
                 .GetCustomAttributes(typeof(DisplayAttribute), false)
                 .Cast<DisplayAttribute>()
                 .SingleOrDefault();

    return (attr != null) ? attr.Name : me.Member.Name;
}

Something like this?

string displayName = GetDisplayName((Dummy x) => x.foo);

// ...

public static string GetDisplayName<T, U>(Expression<Func<T, U>> exp)
{
    var me = exp.Body as MemberExpression;
    if (me == null)
        throw new ArgumentException("Must be a MemberExpression.", "exp");

    var attr = me.Member
                 .GetCustomAttributes(typeof(DisplayAttribute), false)
                 .Cast<DisplayAttribute>()
                 .SingleOrDefault();

    return (attr != null) ? attr.Name : me.Member.Name;
}

Or, if you want to be able to call the method against an instance and take advantage of type inference:

var dummy = new Dummy();
string displayName = dummy.GetDisplayName(x => x.foo);

// ...

public static string GetDisplayName<T, U>(this T src, Expression<Func<T, U>> exp)
{
    var me = exp.Body as MemberExpression;
    if (me == null)
        throw new ArgumentException("Must be a MemberExpression.", "exp");

    var attr = me.Member
                 .GetCustomAttributes(typeof(DisplayAttribute), false)
                 .Cast<DisplayAttribute>()
                 .SingleOrDefault();

    return (attr != null) ? attr.Name : me.Member.Name;
}
怎言笑 2024-12-06 06:22:19

您将需要使用反射。这是一个示例控制台程序,可以执行您想要的操作。

class Program
{
    static void Main(string[] args)
    {
        Dummy dummy = new Dummy();
        PropertyInfo[] properties = dummy.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            IEnumerable<DisplayAttribute> displayAttributes = property.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>();
            foreach (DisplayAttribute displayAttribute in displayAttributes)
            {
                Console.WriteLine("Property {0} has display name {1}", property.Name, displayAttribute.Name);
            }
        }
        Console.ReadLine();
    }
}

public class Dummy
{
    [Display(Name = "Foo")]
    public string foo { get; set; }

    [Display(Name = "Bar")]
    public string bar { get; set; }
}

这将产生以下结果:

http://www.codetunnel.com/content/images /reflectresult.jpg

You will need to use reflection. Here is a sample console program that does what you want.

class Program
{
    static void Main(string[] args)
    {
        Dummy dummy = new Dummy();
        PropertyInfo[] properties = dummy.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            IEnumerable<DisplayAttribute> displayAttributes = property.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>();
            foreach (DisplayAttribute displayAttribute in displayAttributes)
            {
                Console.WriteLine("Property {0} has display name {1}", property.Name, displayAttribute.Name);
            }
        }
        Console.ReadLine();
    }
}

public class Dummy
{
    [Display(Name = "Foo")]
    public string foo { get; set; }

    [Display(Name = "Bar")]
    public string bar { get; set; }
}

This would produce the following result:

http://www.codetunnel.com/content/images/reflectresult.jpg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文