我有一堂课:
namespace MyNamespace
{
public abstract class Class1
{
public static string X()
{
return "Greetings from a method.";
}
}
}
和扩展方法:
namespace MyNamespace
{
public static class Class1Extension
{
public static string Y(this Class1 c1)
{
return "Greetings from extension method.";
}
}
}
当我尝试访问扩展方法时,编译器给了我这个错误:
'MyNamespace.Class1' does not contain a definition for 'Y'
这是我如何使用它:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using MyNamespace;
namespace Test_Console
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Class1.X());
Console.WriteLine(Class1.Y());
Console.ReadLine();
}
}
}