编译器找不到通用类型 class 的 C# 扩展方法
C# extension method for generic type class not found by the compiler
我正在尝试扩展泛型类型 class,但我无法让 VS 查看扩展方法。
当然有很多方法可以解决这个问题,这肯定不是在所有情况下都是最佳做法,但我不明白为什么在下面两个明显相同的情况下,第一个有效,另一个有效没有。
首先,一个成功尝试扩展List的例子class(只是为了证明我可以处理基础知识):
namespace Sandbox.ExtensionsThatWork
{
public static class ListExtensions
{
public static List<TheType> ExtendedMethod<TheType>(this List<TheType> original)
{
return new List<TheType>(original);
}
}
public class ExtensionClient
{
public void UseExtensionMethods()
{
List<string> a = new List<string>();
List<string> b = a.ExtendedMethod();
}
}
}
然而,我要扩展的对象是这样的
namespace Sandbox.Factory
{
public class Factory<T>
{
public static Thing<T> Create()
{
return new Thing<T>();
}
}
public class Thing<T>{}
public static class FactoryExtensions
{
internal static Thing<FactoryType> CreateFake<FactoryType>(this Factory<FactoryType> original)
{
return new FakeThing<FactoryType>();
}
}
public class FakeThing<T> : Thing<T>{}
}
在这种情况下,我一辈子都无法让编译器看到扩展方法。
namespace Sandbox.FactoryClients
{
public class FactoryClient
{
public void UseExtensionMethods()
{
Factory.Thing<int> aThing = Factory.Factory<int>.Create();
///THE COMPILER WON'T FIND THE CreateFake METHOD
Factory.Thing<int> aFakeThing = Factory.Factory<int>.CreateFake<int>();
}
}
}
我错过了什么?
谢谢大家的宝贵时间。
您的问题与泛型无关。
您正在调用扩展,就好像它是 Factory.Factory<int>
的静态方法一样,它 不能是 。
C# 不支持任何类型的扩展 static 方法(意思是扩展方法的行为类似于 this
参数类型的静态方法)。
您需要一个实例来调用扩展方法(就像您在“工作”示例中所做的那样):
using Sandbox.Factory;
public void UseExtensionMethods()
{
Thing<int> aThing = Factory<int>.Create();
Thing<int> aFakeThing = new Factory<int>().CreateFake();
}
我正在尝试扩展泛型类型 class,但我无法让 VS 查看扩展方法。
当然有很多方法可以解决这个问题,这肯定不是在所有情况下都是最佳做法,但我不明白为什么在下面两个明显相同的情况下,第一个有效,另一个有效没有。
首先,一个成功尝试扩展List的例子class(只是为了证明我可以处理基础知识):
namespace Sandbox.ExtensionsThatWork
{
public static class ListExtensions
{
public static List<TheType> ExtendedMethod<TheType>(this List<TheType> original)
{
return new List<TheType>(original);
}
}
public class ExtensionClient
{
public void UseExtensionMethods()
{
List<string> a = new List<string>();
List<string> b = a.ExtendedMethod();
}
}
}
然而,我要扩展的对象是这样的
namespace Sandbox.Factory
{
public class Factory<T>
{
public static Thing<T> Create()
{
return new Thing<T>();
}
}
public class Thing<T>{}
public static class FactoryExtensions
{
internal static Thing<FactoryType> CreateFake<FactoryType>(this Factory<FactoryType> original)
{
return new FakeThing<FactoryType>();
}
}
public class FakeThing<T> : Thing<T>{}
}
在这种情况下,我一辈子都无法让编译器看到扩展方法。
namespace Sandbox.FactoryClients
{
public class FactoryClient
{
public void UseExtensionMethods()
{
Factory.Thing<int> aThing = Factory.Factory<int>.Create();
///THE COMPILER WON'T FIND THE CreateFake METHOD
Factory.Thing<int> aFakeThing = Factory.Factory<int>.CreateFake<int>();
}
}
}
我错过了什么?
谢谢大家的宝贵时间。
您的问题与泛型无关。
您正在调用扩展,就好像它是 Factory.Factory<int>
的静态方法一样,它 不能是 。
C# 不支持任何类型的扩展 static 方法(意思是扩展方法的行为类似于 this
参数类型的静态方法)。
您需要一个实例来调用扩展方法(就像您在“工作”示例中所做的那样):
using Sandbox.Factory;
public void UseExtensionMethods()
{
Thing<int> aThing = Factory<int>.Create();
Thing<int> aFakeThing = new Factory<int>().CreateFake();
}