Tag: generics

如何约束generics类型必须有一个带有某些参数的construtor?

我有一个包装器generics类,旨在与一组类型一起使用。 这些类型由实用程序生成,并且都是从基类ClientBase派生的。 虽然ClientBase只有一个默认构造函数,但所有生成的类型都有默认构造函数,构造函数则将字符串作为参数。 在包装器类的构造函数中,我使用带有字符串的构造函数实例化该类型的实例。 这是一个示例代码: public class ClientBase { } public class GenericProxy where T: ClientBase, new() { T _proxy; public GenericProxy(string configName) { _proxy = new T(configName); } } 此代码无法编译,因为不保证类型T具有接受字符串的构造函数。 有没有办法在generics类上定义约束来强制类型T必须有一个带字符串的构造函数? 如果这是不可能的,那么处理这种情况的好方法是什么?

为什么在通用静态类中声明扩展方法是不可能的?

我想为一些generics类创建很多扩展方法,例如for public class SimpleLinkedList where T:IComparable 我已经开始创建这样的方法: public static class LinkedListExtensions { public static T[] ToArray(this SimpleLinkedList simpleLinkedList) where T:IComparable { //// code } } 但是当我试图使LinkedListExtensions类像这样通用时: public static class LinkedListExtensions where T:IComparable { public static T[] ToArray(this SimpleLinkedList simpleLinkedList) { ////code } } 我得到“扩展方法只能在非generics,非嵌套的静态类中声明”。 而我正在试图猜测这种限制来自哪里并且没有任何想法。 编辑:仍然没有明确的问题愿景。 似乎这只是因为某种原因没有实现。

为什么通用IList 不inheritance非通用IList

IList不inheritanceIList ,其中IEnumerableinheritanceIEnumerable 。 如果out修饰符是唯一的原因,那么为什么大多数IList的实现(例如Collection , List )都实现了IList接口。 所以任何人都可以说好,如果对于IList所有实现都是如此,那么在必要时将其直接转换为IList 。 但问题是虽然IList不inheritanceIList因此无法保证每个IList对象都是IList 。 而且使用IList显然不是解决方案,因为没有out修饰符generics不能分配给较少的inheritance类; 并且创建List的新实例不是解决方案,因为有人可能想要IList实际引用作为IList指针; 并使用List insured IList实际上是一个糟糕的编程实践,并不适用于所有目的。 如果.NET希望提供灵活性, IList每个实现都不应该有非generics实现的契约(即IList )那么为什么他们没有保留另一个实现generics和非generics版本的接口并且没有t建议所有希望签订通用和非遗传项目的具体类别应通过该接口签订合同。 将ICollection为ICollection并将IDictionary为IDictionary时也会出现同样的问题。

如何使用json.net将c#通用列表转换为json?

我正在将我的数据表转换为c#通用列表。 DataTable dt = mydata(); List list = dt.AsEnumerable().ToList(); 现在我如何使用json.net将此list转换为json? 任何建议。 json格式的样本应该是这样的, {“Table” : [{“userid” : “1”,”name” : “xavyTechnologies”,”designation” : “”, “phone” : “9999999999”,”email” : “test@test.com”,”role” : “Admin”,”empId” : “”, “reportingto” : “”},{“userid” : “2”,”name” : “chendurpandian”,”designation” : “softwaredeveloper”,”phone” : “9566643707”,”email” : “chendur.pandia@gmail.com”, “role” : “Super User”,”empId” : “1”,”reportingto” : “xavyTechnologies”}, {“userid” : “3”,”name” : […]

generics不能推断第二个参数?

我注意到C#编译器没有推断出第二个generics参数。 例: C ++模板代码:(我知道模板不像generics那样工作) class Test { public: template T test(V v) { //do something with v return T(); } }; int i = 0; Test t = new Test(); double j = t.test(i); //infers V as int 模板(和generics)不能推断返回类型,所以在C ++中我给它第一个模板参数,第二个模板参数是从变量类型推断出来的。 现在,在C#中使用相同的示例: class Test { public T test(V v) where T: new() { //do something with […]

entity framework – 如何获取列?

我希望获得列名称,类型以及列是否是Entity Framework中表对象的PK的列表。 我如何在C#(4.0)(理想情况下)中执行此操作? 获胜的答案将是有效且最重要的一般答案。

C#:System.Object vs Generics

我很难理解何时使用Object(装箱/拆箱)与何时使用generics。 例如: public class Stack { int position; object[] data = new object[10]; public void Push (object o) { data[position++] = o; } public object Pop() { return data[–position]; } } VS. public class Stack { int position; T[] data = new T[100]; public void Push(T obj) {data[position++] = obj; } public T Pop() { […]

通用Web Api控制器,支持任何模型

是否可以使用支持项目中任何模型的通用Web API? class BaseApiController :ApiController { private IRepository _repository; // inject repository public virtual IEnumerable GetAll() { return _repository.GetAll(); } public virtual T Get(int id) { return _repositry.Get(id); } public virtual void Post(T item) { _repository.Save(item); } // etc… } class FooApiController : BaseApiController { //.. } class BarApiController : BaseApiController { //.. } 这会是一个好方法吗? […]

从base的类静态方法获取派生类类型

我想从其基类​​的静态方法获取派生类的类型。 如何实现这一目标? 谢谢! class BaseClass { static void Ping () { Type t = this.GetType(); // should be DerivedClass, but it is not possible with a static method } } class DerivedClass : BaseClass {} // somewhere in the code DerivedClass.Ping();

为什么Func 与Func <IEnumerable >不明确?

这个让我感到沮丧,所以我想我会在这里问,希望C#guru可以向我解释。 为什么这段代码会产生错误? class Program { static void Main(string[] args) { Foo(X); // the error is on this line } static String X() { return “Test”; } static void Foo(Func<IEnumerable> x) { } static void Foo(Func x) { } } 有问题的错误: Error 1 The call is ambiguous between the following methods or properties: ‘ConsoleApplication1.Program.Foo(System.Func<System.Collections.Generic.IEnumerable>)’ and ‘ConsoleApplication1.Program.Foo(System.Func)’ […]