Tag: extension methods

C#日志设计:扩展方法,替代方案?

我想编写一个可以轻松附加到当前项目中任何类的记录器。 对于开发,将消息记录到控制台会很方便,而在最终版本中,我想记录到文件或其他内容。 一个人应该能够通过编辑几行代码或理想情况下的设置文件来改变行为。 到目前为止我所拥有的是这种类结构: public interface ILogger { void LogMessage(String message); // … other logging functions (exceptions, time etc.) don’t matter here }; public interface ILoggable { }; public static class LoggingProvider { private static ILogger logger = ConsoleLogger.handle; public static void LogMessage(this ILoggable obj, String message) { logger.LogMessage(message); } } public sealed class NullLogger […]

对象深度克隆实现

我必须实现generics扩展deepclone方法,该方法可以与任何引用类型实例一起使用以获取其深层副本。 我实现如下 static class ClassCopy { static public T DeepClone (this T instance) { if (instance == null) return null; var type = instance.GetType(); T copy; var flags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var fields = type.GetFields(flags); // If type is serializable – create instance copy using BinaryFormatter if (type.IsSerializable) { using […]

为什么我的事件处理程序以 c为目标? 另外 – 甚至 c?

遵循创建最小,完整和可validation代码集的规范,请参阅以下内容: using System; using System.Data; using System.Linq; using System.Windows; namespace WhatIsThis{ /// /// Interaction logic for App.xaml /// public partial class App : Application { private void Application_Startup( object sender, StartupEventArgs e ) { Foo.Bar += ( S, E ) => Console.WriteLine( “Do Something!” ); } protected override void OnActivated( EventArgs e ) { base.OnActivated( […]

.NET Micro Framework的扩展方法

似乎扩展方法不支持/使用.NET Micro Framework。 有没有办法让这个有用的语言function工作?

编写具有多种类型的通用扩展方法时的类型推断问题

我正在为IEnumerable编写一个通用扩展方法,用于将对象列表映射到另一个映射对象列表。 这就是我希望该方法工作的方式: IList articles = GetArticles(); return articles.Map(_mappingEngine); 这是方法: public static IEnumerable Map(this IEnumerable list, IMappingEngine engine) { return list.Select(engine.Map); } 但是articles.Map(_mappingEngine); 给出编译错误。 问题是T1的类型推断不起作用。 我必须明确地这样称呼它: articles.Map(_mappingEngine); 如果我创建一个只有一个参数T1的扩展方法,如下所示: public static IEnumerable DummyMap(this IEnumerable list, IMappingEngine engine) { return list; } 然后我可以像这样调用它,而不必指定T1: articles.DummyMap(_mappingEngine); 有没有理由说编译器无法在Map的扩展方法中推断出T1的类型?

如何创建Between Extension方法

我有一个变量,其值在运行时填充。 我想检查该值是否在两个相同的数据类型值之间(比如最低和最高)或不使用扩展方法。 我想检查一下 int a = 2; //here static but is can be changed at runtime if(a.Between(0,8)) DoSomething(); else DoNothing(); 如果a为0或8或它们之间的任何值,则应返回true 。 如果a是(-1或更小)或(9或更大)那么它应该返回false 我想创建一个类似的扩展方法 public static bool Between(this T1 val1, T1 lowest, T1 highest) where ???? { What code to write here???? }

具有相同名称的静态方法和扩展方法

我创建了扩展方法: public static class XDecimal { public static decimal Floor( this decimal value, int precision) { decimal step = (decimal)Math.Pow(10, precision); return decimal.Floor(step * value) / step; } } 现在我尝试使用它: (10.1234m).Floor(2) 但编译器说Member ‘decimal.Floor(decimal)’ cannot be accessed with an instance reference; qualify it with a type name instead Member ‘decimal.Floor(decimal)’ cannot be accessed with an instance […]

智能感知扩展方法?

有没有办法从当前不在使用中但在解决方案中引用的类获取扩展方法的intellisense。 在类中第一次使用扩展方法时,能够只键入并且不必添加使用将是非常方便的。

是否可以使用2.0 Framework创建扩展方法?

我想知道是否有办法使用Visual Studio 2005和2.0框架创建扩展方法? public static class StringExtensions { public static void SomeExtension(this String targetString) { } } 如果没有办法做到这一点,等价物会是什么? 只是在某种库类中创建静态方法?

为什么允许null对象的扩展方法?

允许在null对象上调用扩展方法有什么意义? 这让我不必要地检查扩展方法中的null对象。 AFAIK,我无法理解这一点? 请解释。