为什么我必须写命名空间来访问这个扩展方法?
Why I have to write the namespace to access to this extension method?
我有一个项目 class 可以实现某种类型的扩展方法。例如,对于 ObservableCollection,我有这个 class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace MyProject.Collections.Utils
{
public static class ObservableCollection
{
public static void RemoveAll<T>(this ObservableCollection<T> collection, Func<T, bool> condition)
{
for (int i = collection.Count - 1; i >= 0; i--)
{
if (condition(collection[i]))
{
collection.RemoveAt(i);
}
}
}//RemoveAll
}
}
有了这个 class,在我的主项目中,我可以通过以下方式使用这个库:
using MyProject.Collections.Utils
当我想使用扩展方法时,我可以这样做:
ObservableCollection<MyType> myOC = new ObservableCollection<MyType>();
myOC.RemoveAll(x=>x.MyProperty == "123");
所以我可以访问我的扩展方法。
但是,我还有一个 class 用于十进制,是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyProject.Decimal.Utils
{
public static class Decimal
{
public static decimal? Parse(this string paramString)
{
try
{
myCode
}
catch
{
throw;
}
}//Parse
}
}
但在这种情况下,虽然在我的主要项目中我导入了 class:
using MyProject.Decimal.Utils;
如果我这样做:
decimal? myDecimalParsed= Decimal.Utils.Decimal.Parse("123");
为什么在这种情况下我不能这样做?:
decimal? myDecimalParsed= decimal.Parse("123");
非常感谢。
两个问题:
- 您不能将扩展方法当作扩展类型的静态方法来使用
System.Decimal
已经 有 一个 Parse
方法,编译器总是在扩展方法之前寻找 "real" 方法。
事实上,你可以写
decimal? miTiempoEstimadoParseado = decimal.Parse("123");
...但这只会调用普通方法,然后以普通方式将 decimal
隐式转换为 decimal?
。
请注意,无论如何,您目前并没有真正将您的方法用作扩展方法 - 为此,您应该编写如下内容:
decimal? miTiempoEstimadoParseado = "123".Parse();
...但我个人认为这很丑陋,部分原因是方法名称根本不指示目标类型,部分原因是按照惯例 Parse
方法抛出异常而不是失败时返回空值。您可能想起一个不同的名字。
我有一个项目 class 可以实现某种类型的扩展方法。例如,对于 ObservableCollection,我有这个 class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace MyProject.Collections.Utils
{
public static class ObservableCollection
{
public static void RemoveAll<T>(this ObservableCollection<T> collection, Func<T, bool> condition)
{
for (int i = collection.Count - 1; i >= 0; i--)
{
if (condition(collection[i]))
{
collection.RemoveAt(i);
}
}
}//RemoveAll
}
}
有了这个 class,在我的主项目中,我可以通过以下方式使用这个库:
using MyProject.Collections.Utils
当我想使用扩展方法时,我可以这样做:
ObservableCollection<MyType> myOC = new ObservableCollection<MyType>();
myOC.RemoveAll(x=>x.MyProperty == "123");
所以我可以访问我的扩展方法。
但是,我还有一个 class 用于十进制,是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyProject.Decimal.Utils
{
public static class Decimal
{
public static decimal? Parse(this string paramString)
{
try
{
myCode
}
catch
{
throw;
}
}//Parse
}
}
但在这种情况下,虽然在我的主要项目中我导入了 class:
using MyProject.Decimal.Utils;
如果我这样做:
decimal? myDecimalParsed= Decimal.Utils.Decimal.Parse("123");
为什么在这种情况下我不能这样做?:
decimal? myDecimalParsed= decimal.Parse("123");
非常感谢。
两个问题:
- 您不能将扩展方法当作扩展类型的静态方法来使用
System.Decimal
已经 有 一个Parse
方法,编译器总是在扩展方法之前寻找 "real" 方法。
事实上,你可以写
decimal? miTiempoEstimadoParseado = decimal.Parse("123");
...但这只会调用普通方法,然后以普通方式将 decimal
隐式转换为 decimal?
。
请注意,无论如何,您目前并没有真正将您的方法用作扩展方法 - 为此,您应该编写如下内容:
decimal? miTiempoEstimadoParseado = "123".Parse();
...但我个人认为这很丑陋,部分原因是方法名称根本不指示目标类型,部分原因是按照惯例 Parse
方法抛出异常而不是失败时返回空值。您可能想起一个不同的名字。