Tag: 扩展方法

有没有办法可以从包含代码的字符串中动态定义Predicate主体?

这可能是一个愚蠢的问题,但这里有。 我希望能够从数据库VARCHAR列或任何字符串解析的字符串动态构造谓词。 例如,假设数据库中的列包含以下字符串: return e.SomeStringProperty.Contains(“foo”); 这些代码/字符串值将存储在数据库中,知道通用“e”的可能属性是什么,并且知道它们必须返回布尔值。 然后,在一个神奇,美妙,幻想的世界中,代码可以在不知道谓词是什么的情况下执行,例如: string predicateCode = GetCodeFromDatabase(); var allItems = new List{….}; var filteredItems = allItems.FindAll(delegate(SomeObject e) { predicateCode }); 或Lambda化: var filteredItems = allItems.FindAll(e => [predicateCode]); 我知道它可能永远不会这么简单,但有没有办法,也许使用Reflection.Emit,从文本动态创建委托代码并将其提供给FindAll (或任何其他匿名/扩展)方法?

委托作为扩展方法的第一个参数

女士和男士, 我最近尝试过这个实验: static class TryParseExtensions { public delegate bool TryParseMethod(string s, out T maybeValue); public static T? OrNull(this TryParseMethod tryParser, string s) where T:struct { T result; return tryParser(s, out result) ? (T?)result : null; } } // compiler error “‘int.TryParse(string, out int)’ is a ‘method’, which is not valid in the given context” var […]

c#扩展方法 – 设计模式

我想知道C#扩展方法是否基于任何现有的设计模式。

如何从自己的类调用扩展方法而不进行强制转换?

我试图在我自己的类上调用扩展方法 ,但它无法编译。 考虑以下代码行: public interface IHelloWorld { } public static class Extensions { public static string HelloWorld(this IHelloWorld ext) { return “Hello world!”; } } public class Test : IHelloWorld { public string SaySomething() { return HelloWorld(); } } 基本上我正在扩展界面。 我一直收到这个错误: The name ‘HelloWorld’ does not exist in the current context 有人可以向我解释一下吗? 当我做一个演员表似乎很好: return ((Test)this).HelloWorld(); […]

通用类的扩展方法

可能重复: C# – 通用扩展方法 你如何为一般类型的类编写C#扩展方法 是否可以为generics类声明扩展方法? public class NeedsExtension { public NeedsExtension DoSomething(T obj) { // …. } }

通过扩展方法进行多态性?

我有一个类库,其中包含一些基类和从中派生的其他基类。 在这个类库中,我正在利用多态来做我想做的事情。 现在在一个消费应用程序中,我想根据子类的运行时类型更改某些代码的行为。 假设如下: public class Base { } public class Child1 : Base { } public class Child2 : Base { } 现在在消费应用程序中,我想做一些如下操作(请注意,以下所有类都在使用应用程序中,并且不能在类库中引用): public interface IMyInterface1 { } public interface IMyInterface2 { } public static class Extensions { public static void DoSomething(this Base myObj, Object dependency) { } public static void DoSomething(this Child1 myObj, […]

如何制作字典扩展方法?

我正在尝试编写一个独立于Key / Value数据类型的Dictionary扩展。 我尝试使用object数据类型传递它,假设它适用于任何类型。 我的代码: public static class DictionaryExtensionsClass { public static void AddFormat(this Dictionary Dic, ??unknow type/*object*/ Key, string str, params object[] arglist) { Dic.Add(Key, String.Format(str, arglist)); } }

为什么foreach无法找到我的GetEnumerator扩展方法?

我试图使一些代码更具可读性。 例如foreach(var row in table) {…}而不是foreach(DataRow row in table.Rows) {…} 。 为此,我创建了一个扩展方法: namespace System.Data { public static class MyExtensions { public static IEnumerable GetEnumerator( this DataTable tbl ) { foreach ( DataRow r in tbl.Rows ) yield return r; } } } 但是编译器仍然抛出foreach statement cannot operate on variables of type ‘System.Data.DataTable’ because ‘System.Data.DataTable’ does not […]

如何在VB代码中调用C#扩展方法

我有一个类库,其中包含一些用C#编写的扩展方法和一个用VB编写的旧网站。 我想从VB代码中调用我的扩展方法,但它们没有出现在intelisense中,当我访问该站点时出现编译错误。 我已经获得了所有必需的Import,因为同一名称空间中包含的其他类在Intelisense中看起来很好。 有什么建议 编辑:更多信息,以帮助一些评论。 我的实现看起来像这样 //C# code compiled as DLL namespace xy { public static class z { public static string q (this string s){ return s + ” ” + s; } } } 和我这样的用法 Imports xy ‘…’ Dim r as string = “greg” Dim s as string = rq() ‘ does not […]

为什么LINQ to SQL不支持查询运算符’ElementAt’?

在LINQ to SQL中,我得到exception“ 不支持查询运算符’ElementAt’。 ”当尝试在从LINQ to SQL查询返回的IQueryable上使用ElementAt扩展方法时。 这是堆栈跟踪: at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.ElementAt[TSource](IQueryable`1 source, Int32 index) 现在我意识到要摆脱这个exception并使用ElementAt我可以在使用扩展方法之前调用’.ToList()’它会起作用。 这很好,但我仍然不喜欢这是一个运行时exception(看起来像LSP违规)的事实。 有没有理由不支持这些方法? 是不是因为它们无法轻易转换为SQL? 不支持哪些其他IQueryable / IEnumerable扩展方法,是否有某个列表? 避免运行时exception会很好。