擴展方法我有下面的類層次結構的強類型屬性訪問
class Test
{
public string Name { get; set; }
}
class TestChild : Test
{
public string Surname { get; set; }
}
我不能更改測試類。我想寫下面的擴展方法是這樣的:
static class TestExtensions
{
public static string Property<TModel, TProperty>(this Test test, Expression<Func<TModel, TProperty>> property)
{
return property.ToString();
}
}
爲了能夠以如下方式使用它:
class Program
{
static void Main(string[] args)
{
TestChild t = new TestChild();
string s = t.Property(x => x.Name);
}
}
但現在編譯器說
的方法類型參數'ConsoleApplication1.TestExtensions.Property(ConsoleApplication1.Test,System.Linq.Expressions.Expression>)'不能從使用情況中推斷出來。嘗試明確指定類型參數。
我想要像mvc Html.TextBoxFor(x => x.Name)方法。 是否可以寫入擴展名,如Main
方法所示?
我錯過了這個第一次:( – poindexter12 2010-12-13 18:38:09
此外,我不認爲該property.ToString()是正確的在那裏做。 – poindexter12 2010-12-13 18:46:19