2012-04-19 51 views
2

您好我想知道是否有直接的方式來檢索我的模型VIA控制器中的自定義屬性的值。對於arugment緣故...讓我們說我有這個在我的模型:從控制器獲取MVC2模型屬性值

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

在我的控制器中我想用一些類似的檢索「名稱」:

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

有什麼事情稀奇的?

在此先感謝。
WML

+0

對另一個堆棧溢出問題的這個答案可能會幫助你http://stackoverflow.com/a/3289235/333082 – cecilphillip 2012-04-19 04:27:25

回答

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我的答案?如果我不知道問題是什麼,我無法修復任何感知錯誤... – 2012-04-19 04:23:06

+0

感謝您的靈感答案....我創建了一個基於[鏈接] http://msdn.microsoft.com的例程/en-us/library/system.attribute(v=vs.90).aspx文章並使用反射來詢問propertyInfo。我讀了更多關於GetCustomAttributes的內容,基本上和你的例程類似。非常感謝。 – WML 2012-04-20 01:06:43

1

試試這個:

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

這是我的不好,但如果該屬性是一個自定義屬性,而不是DisplayName呢?我試圖做的是在我的模型類中對一個屬性(例如Test)設置一個自定義屬性[GroupId(2)]。在這種情況下,我試圖說「測試有一個2的groupId」。我可以通過簡單地創建另一個屬性來實現同樣的目的,但我認爲將它作爲一個屬性可以更簡潔。 我對不屬於屬性的概念非常熟悉的道歉。應該讀更多。 – WML 2012-04-19 23:32:08

1

這是很容易與反思的事情。 內部控制器:

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


    } 

擴展:

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; 
    } 

} 

我測試了它工作正常。它將使用該屬性的第一個屬性。 MailJobView類具有名爲「Name」的DisplayNameAttribute屬性。