相关疑难解决方法(0)

反射 - 获取属性的属性名称和值

我有一个类,让我们用名为Name的属性调用它.使用该属性,我有一个与之关联的属性.

public class Book
{
    [Author("AuthorName")]
    public string Name
    {
        get; private set; 
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的main方法中,我正在使用反射并希望为每个属性获取每个属性的键值对.所以在这个例子中,我希望看到属性名称为"Author",属性值为"AuthorName".

问题:如何使用Reflection获取属性的属性名称和值?

c# reflection propertyinfo

225
推荐指数
7
解决办法
28万
查看次数

资源中的DisplayName属性?

我有一个本地化的应用程序,我想知道是否可以DisplayName从资源中设置某个模型属性.

我想做这样的事情:

public class MyModel {
  [Required]
  [DisplayName(Resources.Resources.labelForName)]
  public string name{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但是我不能这样,因为编译器说:"属性参数必须是常量表达式,typeof表达式或属性参数类型的数组创建表达式":(

有没有解决方法?我手动输出标签,但我需要这些用于验证器输出!

c# asp.net-mvc resources localization resx

159
推荐指数
6
解决办法
12万
查看次数

C#反射:在成员字段上查找属性

我可能会错误地问这个问题,但可以/如何在自己的课程中找到字段......例如......

public class HtmlPart {
  public void Render() {
    //this.GetType().GetCustomAttributes(typeof(OptionalAttribute), false);
  }
}

public class HtmlForm {
  private HtmlPart _FirstPart = new HtmlPart();      
  [Optional] //<-- how do I find that?
  private HtmlPart _SecondPart = new HtmlPart();
}
Run Code Online (Sandbox Code Playgroud)

或者我可能只是错误地执行此操作...如何调用方法然后检查应用于自身的属性?

此外,为了这个问题 - 我很好奇是否有可能在不知道/访问父类的情况下找到属性信息!

c# reflection attributes field

16
推荐指数
3
解决办法
2万
查看次数

如何通过Reflection获取属性的DisplayAttribute?

我有一个像这样的Helper方法来获取PropertyName(试图避免魔术字符串)

public static string GetPropertyName<T>(Expression<Func<T>> expression)
        {
            var body = (MemberExpression) expression.Body;
            return body.Member.Name;
        }
Run Code Online (Sandbox Code Playgroud)

但有时我的PropertyNames也没有好好命名.所以我想宁愿使用DisplayAttribute.

[Display(Name = "Last Name")]
public string Lastname {get; set;}
Run Code Online (Sandbox Code Playgroud)

请注意我使用的是Silverlight 4.0.我无法找到通常的命名空间DisplayAttributeName属性.

如何更改我的方法来读取eproperty的属性(如果可用)?

非常感谢,

.net c# reflection silverlight

12
推荐指数
1
解决办法
8027
查看次数

如何获取元数据自定义属性?

我有一个类在类级别定义数据注释.元数据类具有与之关联的自定义属性,以及通常的DisplayName,DisplayFormat等.

public class BaseMetaData
{
    [DisplayName("Id")]
    public object Id { get; set; }

    [DisplayName("Selected")]
    [ExportItem(Exclude = true)]
    public object Selected { get; set; }
}

[MetadataType(typeof(BaseMetaData))]
public class BaseViewModel
{
    public int Id { get; set; }
    public bool Selected { get; set; }
Run Code Online (Sandbox Code Playgroud)

给定类型T,如何从元数据类中检索自定义属性?下面的尝试不起作用,因为元数据属性来自BaseViewModel而不是BaseMetaData类.

需要一般工作,即不能做typeof(BaseMetaData).GetProperty(e.PropertyName).想知道是否有从类中获取MetadataType的方法,那么它将使其成为可能.

var type = typeof (T);
var metaData = ModelMetadataProviders.Current.GetMetadataForType(null, type);

var propertMetaData = metaData.Properties
    .Where(e =>
    {
        var attribute = type.GetProperty(e.PropertyName)
            .GetCustomAttributes(typeof(ExportItemAttribute), false)
            .FirstOrDefault() as ExportItemAttribute; …
Run Code Online (Sandbox Code Playgroud)

c# reflection data-annotations

5
推荐指数
1
解决办法
1万
查看次数

如何在MVVM中捕获DataAnnotations验证

我如何从DataAnnotations中捕获验证?我在这里研究,但我不明白它是如何工作的

所以我跳了一些你可以启发我的

这是我目前的测试代码:

模型

public class Person // Represents person data.
{
    /// <summary>
    /// Gets or sets the person's first name.
    /// </summary>
    /// <remarks>
    /// Empty string or null are not allowed.
    /// Allow minimum of 2 and up to 40 uppercase and lowercase.
    /// </remarks>
    [Required]
    [RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")]        
    public string FirstName{ get; set;}

    /// <summary>
    /// Gets or sets the person's last name.
    /// </summary>
    /// <remarks>
    /// Empty string or null are not allowed.
    /// </remarks>
    [Required] …
Run Code Online (Sandbox Code Playgroud)

c# validation attributes mvvm data-annotations

1
推荐指数
1
解决办法
4388
查看次数

为什么我不能将DisplayAttribute与Global Resource文件一起使用?

问题已经解释了我正在尝试做什么,这是一个例子:

[Display(Name = Localization.City)]
public string City { get; set; }
Run Code Online (Sandbox Code Playgroud)

错误是,但(对我来说)没有意义:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

c# asp.net resources

0
推荐指数
1
解决办法
1724
查看次数