假設你指的是參數是自定義的屬性屬性類:
class Program
{
static void Main(string[] args) {
Test();
Console.Read();
}
[Custom(Foo = "yup", Bar = 42)]
static void Test() {
// Get the MethodBase for this method
var thismethod = MethodBase.GetCurrentMethod();
// Get all of the attributes that derive from CustomAttribute
var attrs = thismethod.GetCustomAttributes(typeof(CustomAttribute), true);
// Assume that there is just one of these attributes
var attr1 = (CustomAttribute)attrs.Single();
// Print the two properties of the attribute
Console.WriteLine("Foo = {0}, Bar = {1}", attr1.Foo, attr1.Bar);
}
}
class CustomAttribute : Attribute
{
public string Foo { get; set; }
public int Bar { get; set; }
}
注意,屬性是有點特殊的,因爲他們可以採取命名參數(對應於公共屬性名稱),沒有聲明任何構造函數。
請注意'param1'和'param2'只是'SomeCustomAttr'構造函數的參數。假設您的ctor將這些值分配給屬性,您只需按照該問題的信息,並在獲取屬性對象後訪問這兩個屬性。 –