2012-04-19 21 views
2

こんにちは、私のモデルのカスタム属性の値をコントローラから取得するための直接的な方法があるのだろうかと思います。 arugmentために...のは、私は私のモデルでこれを持っているとしましょう:私のコントローラでコントローラからMVC2モデルの属性値を取得

[DisplayName("A name")] 
public string test; 

私はこれに似たものを使用して、「名前」を取得したい:

ModelName.test.Attributes("DisplayName").value 

それは何かです想像を絶する?

ありがとうございます。
WML

+0

他のスタックオーバーフローの質問へのこの回答はあなたを助けるかもしれないhttp://stackoverflow.com/a/3289235/333082 – cecilphillip

答えて

3

Here is a good article on how to retrieve values from attributes.これを反映させるための方法は他にありません。

記事(ちょうど:)あなたの例のための属性タイプを変更)から:

public static void PrintAuthorInfo(Type t) 
    { 
     Console.WriteLine("Author information for {0}", t); 
     Attribute[] attrs = Attribute.GetCustomAttributes(t); 
     foreach(Attribute attr in attrs) 
     { 
     if (attr is Author) 
     { 
      Author a = (Author)attr; 
      Console.WriteLine(" {0}, version {1:f}", 
a.GetName(), a.version); 
     } 
     } 
    } 
+0

匿名のdownvoterに私の答えをdownvotedした理由を教えてください?問題が何であるかわからないと、私は認識されたエラーを修正できません... –

+0

インスピレーションのお返事ありがとうございました....私は[リンク] http://msdn.microsoft.comに基づいてルーチンを作成しました/en-us/library/system.attribute(v=vs.90).aspxの記事を読んで、reflectionを使ってpropertyInfoを調べます。私はGetCustomAttributesの詳細を読んで、基本的にあなたのルーチンに似ています。どうもありがとう。 – WML

1

これを試してみてください:

var viewData = new ViewDataDictionary<MyType>(/*myTypeInstance*/); 
string testDisplayName = ModelMetadata.FromLambdaExpression(t => t.test, viewData).GetDisplayName(); 
+0

それは私の悪いですが、属性がDisplayNameではなくカスタム属性の場合はどうなりますか?私がしようとしていたのは、自分のモデルクラスのプロパティ(例:Test)に対してカスタム属性[GroupId(2)]を持たせることでした。この場合、私は "Test has groupId 2"を関連づけようとします。私は単に別のプロパティを作成するだけで同じことを達成できますが、それを属性として行うのはちょっときちんとしていると思います。 属性の概念にあまり慣れていない私の謝罪。もっと読むべきです。 – WML

1

反射して行うのは簡単です。私はそれが正常に動作テスト

public static class AttributeSniff 
{ 
    public static string Attributes<T>(this object inputobject, string propertyname) where T : Attribute 
    { 
     //each attribute can have different internal properties 
     //DisplayNameAttribute has public virtual string DisplayName{get;} 
     Type objtype = inputobject.GetType(); 
     PropertyInfo propertyInfo = objtype.GetProperty(propertyname); 
     if (propertyInfo != null) 
     { 
      object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(T), true); 

      // take only publics and return first attribute 
      if (propertyInfo.CanRead && customAttributes.Count() > 0) 
      { 
       //get that first one for now 

       Type ourFirstAttribute = customAttributes[0].GetType(); 
       //Assuming your attribute will have public field with its name 
       //DisplayNameAttribute will have DisplayName property 
       PropertyInfo defaultAttributeProperty = ourFirstAttribute.GetProperty(ourFirstAttribute.Name.Replace("Attribute","")); 
       if (defaultAttributeProperty != null) 
       { 
        object obj1Value = defaultAttributeProperty.GetValue(customAttributes[0], null); 
        if (obj1Value != null) 
        { 
         return obj1Value.ToString(); 
        } 
       } 

      } 

     } 

     return null; 
    } 

} 

public void TestAttribute() 
    { 
     MailJobView view = new MailJobView(); 
     string displayname = view.Attributes<DisplayNameAttribute>("Name") ; 


    } 

拡張子:コントローラ内部 。そのプロパティの最初の属性が使用されます。 MailJobViewクラスには、DisplayNameAttributeで "Name"という名前のプロパティがあります。

関連する問題