Tag: generics

Generic Func 作为基本方法的参数

我可能会失去情节,但我希望有人可以指出我正确的方向。 我想做什么? 我正在尝试编写一些基本方法,这些方法采用Func 和Action,以便这些方法处理所有异常处理等,因此它不会在所有地方重复,但允许派生类指定它想要执行的操作。 到目前为止,这是基类。 public abstract class ServiceBase { protected T Settings { get; set; } protected ServiceBase(T setting) { Settings = setting; } public void ExecAction(Action action) { try { action(); } catch (Exception exception) { throw new Exception(exception.Message); } } public TResult ExecFunc(Func function) { try { /* what goes here?! */ […]

为什么’Func ‘编译’Func ,其中TGeneric:IBase’没有?

为什么下面的集团错了? public interface IBase { } public class ClassX : IBase { } public class ClassY { public static ClassX FunctionReturnX() { return new ClassX(); } } public class ClassZ where TGeneric : IBase { Func funcInterface = ClassY.FunctionReturnX; //Right Func funcGeneric = ClassY.FunctionReturnX; //Wrong }

通过reflection / typename实例化generics类

例如,我有一个类,看起来像: public class Repository { public IEnumerable FindAll() { // } } 我希望能够做的是通过reflection创建一个Repository实例。 例如: var typeName = “Customer” var type = Assembly.GetCallingAssembly().GetType(typeName); //obviously, this isn’t valid… var repository = new Repoistory(); 沿着这些方向的事情可能吗?

如何从具有generics返回类型的方法返回基元?

在我的代码中,我有一个通用的“getProperty”,如下所示: public T getProperty(int GUID, string property) { PropertyComponent prop; prop = propDict[GUID]; if(property.Equals(“visible”)) return (T) (Boolean) prop.visible; if(property.Equals(“enabled”)) return prop.enabled; if(property.Equals(“position”)) return (T) (Object) prop.position; } Visual Studio的编译器在第3个术语中没有给出任何错误,因为prop.position是Vector2。 然而,Prop.visible和prop.enabled是bool s,所以当我尝试以这种方式返回它时,我得到一个错误“无法将类型bool转换为T”(并且“无法将类型bool隐式转换为T”第二)。 在这种情况下返回布尔的正确方法是什么?

链接通用列表扩展的方法

我有一个“复杂”类型的列表 – 一个具有一些字符串属性的对象。 List本身是另一个对象的属性,包含各种类型的对象,如此缩写类结构所示: Customer { public List Characteristics; . . . } Characteristic { public string CharacteristicType; public string CharacteristicValue; } 我希望能够为当前客户收集特定类型特征值的列表,我可以按如下步骤分两步完成: List interestCharacteristics = customer.Characteristics.FindAll( delegate (Characteristic interest) { return interest.CharacteristicType == “Interest”; } ); List interests = interestCharacteristics.ConvertAll( delegate (Characteristic interest) { return interest.CharacteristicValue; } ); 这很好,但似乎还有很长的路要走。 我确定我必须错过一个更简单的方法来获取这个列表,或者通过链接FindAll()和Convert()方法,或者我完全忽略的其他东西。 对于背景,我在.Net 2.0工作,所以我只限于.Net 2generics,而且特性类是外部依赖 – […]

通过通用方式创建任何C#类的intance

我想使用generics创建任何类的实例。 那可能吗? 我试过这个但是没有用: public class blabla { public void bla(); } public class Foo { Dictionary<string, Func> factory; public Foo() => factory = new Dictionary<string, Func>(); public WrapMe(string key) => factory.Add(key, () => new T()); } … var foo = new Foo(); foo.Wrapme(“myBlabla”); var instance = foo.factory[“myBlabla”]; instance.Bla();

如何在Autofac中为开放式通用注册注册一个开放的通用装饰器?

我有一个autofac处理程序的开放式通用注册,看起来像这样。 builder.RegisterAssemblyTypes(assemblies) .AsClosedTypesOf(typeof (ICommandHandler)) .AsImplementedInterfaces(); 这工作正常,并注册所有我的处理程序,用于封闭类型。 我现在想为所有处理程序注册一个通用装饰器,例如a LoggingCommandHandlerDecorator 从autofac文档来看,您需要为实现命名,因此装饰器可以是默认的ICommandHandler。 在注册开放式generics时,我不确定这是如何工作的。 我试过在开放注册中添加一个名字。 builder.RegisterAssemblyTypes(assemblies) .AsClosedTypesOf(typeof (ICommandHandler)) .Named(“commandHandler”, typeof (ICommandHandler)) .AsImplementedInterfaces(); 并注册装饰,但没有喜悦。 builder.RegisterGenericDecorator(typeof (LoggingCommandHandlerDecorator), typeof (ICommandHandler), fromKey: “commandHandler”); 任何帮助赞赏。

C#以List作为参数插入到SQL表中

在这个精彩论坛的专家的真诚帮助下,我已经能够解析由SharePoint列表返回的我的xml,以将所需的列表项目转换为C#列表。 XDocument xdoc = XDocument.Parse(activeItemData.InnerXml); XNamespace z = “#RowsetSchema”; List listID = (from row in xdoc.Descendants(z + “row”) select (int)row.Attribute(“ows_ID”)).ToList(); List listTitle = (from row in xdoc.Descendants(z + “row”) select (string)row.Attribute(“ows_LinkTitle”)).ToList(); 我创建了一个SQL表,我想使用Lists listID和listTitle作为参数在我的表中插入值 System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(MyconnectionString); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = “INSERT TechOpsProjectTracker (ID, [Project Name]) VALUES (@ID, […]

通用Linq DataContext

假设我有一个名为User的表。 使用LINQ desinger,我将得到以下结果: 一个名为User.dbml的文件 一个名为UserDataContext的数据上下文类,它是System.Data.Linq.DataContext的子类 一个名为User的类,它从User表映射。 UserDataContext对象将具有名为Users的属性,其类型为System.Data.Linq.Table 。 到现在为止还挺好。 现在我想定义一个通用基类,它将Linq.Table属性转换为所有子类的JSON字符串。 所以我会: using Newtonsoft.Json; class BasePlugin where T : System.Data.Linq.DataContext, new() { protected T DataContext = new T(); protected string GetJSONData() { //*** DataContext if of type System.Data.Linq.DataContext, therefore it won’t know the Linq Table property of its subclasses return JsonConvert.SerializeObject(DataContext.Cannot_get_subclass_property_Linq_table); } } 要完成问题中的代码,这是一个子类的示例: class UserPlugin […]

设计Fluent界面方法

我正在尝试编写DSL 我有返回字符串的方法,但如果我想组合字符串,我需要使用+符号,但我想一起调用这些方法,但我不确定如何实现它 我现在有方法如 MyStaticClass.Root() MyStaticClass.And() MyStaticClass.AnyInt()返回字符串的MyStaticClass.Root() MyStaticClass.And() MyStaticClass.AnyInt() 我希望能够做到 Root().And().AnyInt()导致字符串