Tag: generics

以类型通用/不可知方式实现generics方法的最佳方法

假设我们有一个带有这种签名的generics方法: T Obfuscate(T value) where T : IConvertible 我正在为IConvertible设置类型约束,所以这个方法可以消化简单的值类型以及字符串。 让我们忘记一下,也可以提供枚举…… 我想避免这样的实现,它将检查实际的参数类型以执行正确的处理。 // Please no GOD METHODS public T Obfuscate(T value) where T : IConvertible { if (value is int) { … } if (value is string) { … } } 这肯定是工厂方法的气味,必须调用特定的实现提供程序,但仍然需要进行类型检查。 对于这种情况,您认为最好的(希望是通用方法)是什么? 为什么是通用方法? 我决定使用generics方法,因此它总是返回正确的类型,而不需要在调用代码时强制转换方法。

如何在C#中的类型安全集合中存储具有约束generics类型的Action委托?

假设我想将代理存储在这样的集合中: public void AddDelegate(Action action) where T : ISomething { _delegates.Add(action); } _delegates的类型应该是什么? 尝试使用IList<Action> _delegates; 导致错误消息Argument type ‘System.Action’ is not assignable to parameter type ‘System.Action’上面的Add调用的Argument type ‘System.Action’ is not assignable to parameter type ‘System.Action’ 。 但为什么不起作用? 编译器应该“知道”T必须是ISomething 。 如果我不想通过使用例如IList或参数化generics类型T上的整个类来放弃类型安全,我有哪些选择?

如何从堆栈框架中获取generics参数的类型?

我们应该通过工厂实例化我们的实体,因为它们在客户端和服务器上的设置不同。 我想确保这种情况,但不能让它工作。 public interface IEntityFactory { TEntity Create() where TEntity : new(); } public abstract class Entity { protected Entity() { VerifyEntityIsCreatedThroughFactory(); } [Conditional(“DEBUG”)] private void VerifyEntityIsCreatedThroughFactory() { foreach (var methodBase in new StackTrace().GetFrames().Select(x => x.GetMethod())) { if (!typeof(IEntityFactory).IsAssignableFrom(methodBase.DeclaringType) || methodBase.Name != “Create”) continue; // The generic type is TEnitiy but I want the provided […]

Generics 无法在c#中运行,甚至没有任何错误

我正在使用Silverlight 5(VS 2010)创建一个C#Web应用程序。 首先,我创建了控制台应用程序,因为每件事都运行良好。 但是当我在Web应用程序中执行此操作时会出现问题 即使在Web应用程序中,它也适用于特别设置的数据类型(例如,对于int而不是它工作正常)但是当我使用generics时它不起作用。 它编译无错误,但它甚至不调试设置为“切换断点”的区域。 最初GUI是这样的: 但是当控件传递到容易出错的部分时,GUI会突然消失 而且我保留断点的地方被这个替换了 (参见最左边的) 因此我无法调试以找到问题 。 我正在尝试做的一些解释:在下面的给定代码中我有一个二进制文件并存储在“fileContents”中,它是数据类型byte[] (我现在没有向您透露读取该文件的方法,你可以认为fileContents包含MainPage类中的二进制文件的内容。 实际上我会将符号(forms为0和1的符号存储在二进制文件中)并找到它的频率(通过计算它在文件中重复的次数,但没有问题所以我没有为它编写方法)。 但是我的代码中的这个processingValue变量将是generics类型( ),我将它存储在”symbol” (也是类型的(从二进制文件读取的这个符号可能是这些short / int /之一)我没有在我的代码中显示的long / UInt32 / UInt64等)。 我有这样的场景: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.IO; using System.Text; using System.Runtime.InteropServices; […]

如何避免冗余代码没有不必要的genericsgenerics

在问题“ C#generics:将generics类型转换为值类型 ”中,Eric Lippert和Jon Skeet都表示以下方法不是一个好的解决方案 //return new T { BusinessType = (U)(object)this }; //Eric Lippert answer (Not suggested) //return new T { BusinessType = (dynamic)this }; //Jon Skeet answer (Not suggested) 我的C#.Net 4.0解决方案中存在code redundancy问题。 在EngineDesignPatent和BenzolMedicinePatent重复了GetCalculator()方法。 public override InvestmentReturnCalculator GetCalculator() { IntellectualRightsInvestmentReturnCalculator c = new IntellectualRightsInvestmentReturnCalculator(); c.BusinessType = this; return c; } 虽然不是更可取,但@Kit提到了以下方法来避免“ 重构代码以避免类型转换 ”中的冗余。 //BusinessBaseClass […]

为什么不能推断出这些generics类型参数?

给定以下接口/类: public interface IRequest { } public interface IHandler where TRequest : IRequest { TResponse Handle(TRequest request); } public class HandlingService { public TResponse Handle(TRequest request) where TRequest : IRequest { var handler = container.GetInstance<IHandler>(); return handler.Handle(request); } } public class CustomerResponse { public Customer Customer { get; set; } } public class GetCustomerByIdRequest : […]

如何以通用方式写下面的逻辑?

我有一个如下的模型 public sealed class Person { public string MobileNo { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } } 在我的implmentation类中,我有一个方法,它将IEnumerable作为参数 public string PersonList(string listName, IEnumerable persons) { dictionary.Add(“name”, new String[1] { listname }); dictionary.Add(“list”, persons.ToArray()); PrivateMethod(“personList”, dictionary); } 我有另一种私人方法 private string PrivateMethod(string value, Dictionary parameters) { […]

将generics参数转换为整数和从整数转换

我想编写generics类,它用于内置类型,如byte和ushort 。 在内部计算中,我需要将generics类型转换为整数并返回generics类型。 我找到了编译此类代码的方法,例如: class Test where T : struct, IConvertible { public static T TestFunction(T x) { int n = Convert.ToInt32(x); T result = (T)Convert.ChangeType(n, typeof(T)); return result; } } 我认为如果将它们用于计算循环中,使用此类转换可能会显着降低性能。 有没有更好的方法来进行这些转换?

设计多类型对象的最佳方法

假设我有一个数据对象,但是这个对象可以包含几种类型的数据之一。 class Foo { int intFoo; double doubleFoo; string stringFoo; } 现在,我想创建一个访问器。 获取此数据的一些方法。 显然,我可以创建多个访问者: public int GetIntFoo(); public double GetDoubleFoo(); public string GetStringFoo(); 或者我可以创建多个属性 public int IntFoo { get; set; } public double DoubleFoo { get; set; } public string StringFoo { get; set; } 我不认为这是一个非常好的设计。 它要求客户端代码更关心类型而不是它本来应该的。 更重要的是,我真的只需要这个类的单个值,上面的内容将允许同时分配每种类型中的一个。 不好。 一种选择是使用generics。 class Foo { public T […]

使用c#在asp.net中排序列表和下拉列表

我有一个方法,它返回一个sortedList,我想将它数据源到Dropdownlist。 我在用 DropDownList1.DataSource=stList; DropDownList1.DataValueField=stList.ContainsValue(); DropDownList1.DataTextField=stList.ContainsKey(); DropDownList1.DataBind(); 但它给出了一个错误:containsKey和containsValue没有重载方法。 如何在下拉列表中填充此已排序的表?