【c# – 使用泛型问题并使用运行时类型而不是(通用)T】教程文章相关的互联网学习教程文章

c# – Delegate.CreateDelegate无法绑定到静态泛型方法【代码】

我正在尝试使用Delegate.CreateDelegate [MSDN link]绑定到静态泛型方法,但绑定失败.这是PoC代码:public static class CreateDelegateTest {public static void Main() {Action actionMethod = CreateDelegateTest.GetActionDelegate();Action<int> intActionMethod = CreateDelegateTest.GetActionDelegate<int>();Func<int> intFunctionMethod = CreateDelegateTest.GetFunctionDelegate<int>();}public static Action GetActio...

有没有办法让C#泛型处理这种情况【代码】

当遇到以下情况时,我对使用该行乱七八糟的消耗代码感到不满意var queryResult = _queryDispatcher.Dispatch<CustomerByIdQuery, CustomerByIdQueryResult>(customerByIdQuery).Customer;我希望代码以这种方式为消费者工作:var queryResult = _queryDispatcher.Dispatch(customerByIdQuery).Customer;有没有办法使用泛型来实现这一目标? 这是代码interface IQuery{}interface IQueryResult{}interface IQueryHandler<TQuery, TQue...

c# – 如何在MVVMCross中注册泛型类型【代码】

我有一个接口定义如下:public interface ISerializationStrategy<T>还有一个通用的实现:public class SerializationStrategy<T> : Core.Strategies.ISerializationStrategy<T>当我在IOC中注册时,我必须为每种类型执行以下操作:Mvx.RegisterType<ISerializationStrategy<IdentityProvider>,SerializationStrategy<IdentityProvider>>(); ... each type或者有没有办法注册一个开放的通用(我认为这就是所谓的).以下不起作用:Mvx.R...

从Type with Reflection获取类并使用Type in C#调用泛型构造函数【代码】

我正在使用Dapper,我想迭代我的模型类,并为任何具有ColumnAttribute修饰的字段的类设置类型映射.public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper {public static readonly string ColumnAttributeName = "ColumnAttribute";public ColumnAttributeTypeMapper(): base(new SqlMapper.ITypeMap[]{new CustomPropertyTypeMap(typeof (T), SelectProperty),new DefaultTypeMap(typeof (T))}){}// implementation of S...

c# – XML反序列化泛型类型的项【代码】

假设我有以下课程:public abstract class ScheduledService : ScheduledServiceBase<ScheduledService> {public CronInfo CronInfo;public String ServiceName;public ScheduledService(){ } }public abstract class ScheduledServiceBase<T> {public ScheduledServiceBase(){ }public virtual void StartUp(IScheduler scheduler, ScheduledService service, Dictionary<string, object> parameters = null){...} }从这个基类我创...

c# – 使用typeof作为泛型类型参数【代码】

根据typeof的文档:[…] To obtain the run-time type of an expression, you can use the .NET Framework method GetType因此,这意味着typeof必须是编译时表达式. 在关于Generic Type Parameters的文章中,声明:[…] The type argument for this particular class can be any type recognized by the compiler.编译器识别的任何类型都是编译器推断的任何类型,即编译时已知的任何类型. 如果这是真的,那么为什么不允许以下语句?int...

c# – 以小端方式整数到字节数组(泛型函数)【代码】

我有这个方法来转换长到小端字节数组public static byte[] UnsignedIntegerToLEByteArray(ulong value) { // Value in bytes... in your system's endianness (let's say: little endian)byte[] bytes = BitConverter.GetBytes(value);// If it was big endian, reverse itif (!BitConverter.IsLittleEndian)Array.Reverse(bytes); return bytes; }我的目标是将它用于短路数据类型,如int,short等.请看这里:byte a = 0xAA; usho...

c# – 实现从非泛型接口继承的通用接口【代码】

我知道如何实现一个继承非通用接口的通用接口,但是在这个过程中有一些我不理解的东西: 为什么在实现接口的类中不能将非泛型接口方法声明为public? 一个例子:interface InputTestSocket{object GetValue();}interface InputTestSocket<T> : InputTestSocket{new T GetValue();}class FloatInputSocketValues : InputTestSocket<float>{IFloatConvertable _output;public void SetConvertable(IFloatConvertable output) { _outpu...

c# – 为什么不允许指向泛型类型的指针?【代码】

例如,作为一个重载的[] setter-getter,public T this[int i]{get{unsafe{T* p = (T*)hArr.ToPointer(); // hArr is a C++ object pointer(IntPtr)return *(p + i);}}set{unsafe{T* p = (T*)hArr.ToPointer();*(p + i) = value;}}}和编译器抱怨(强调)关于它“不能将…的地址”带到托管类型T. 我知道T在运行时只会是float,double,int或byte,但我不知道如何告诉编译器,所以它信任我. 为什么我不能使用它,无论如何都是指针,如果我不小心...

c# – 使用泛型类和接口实现抽象工厂【代码】

我想实现一个抽象工厂(带有单例)并在我的代码中使用它,并将TType和TInterfaceType的具体实例映射到. 这是我目前的代码:public abstract class AbstractFactory<TType, TInterfaceType> where TType : new() where TInterfaceType : class {private TInterfaceType objectTtype;public TInterfaceType getInstance(){try{if (objectTtype == null){objectTtype = new TType();}return objectTtype;}catch (Exception e){throw e;}}...

方法参数C#中的有界泛型类型【代码】

问题:C#是否支持方法参数中的有界泛型类型?如果是这样,语法是什么? 上下文:我正在编写一个实用程序方法,对字典中的值进行字典排序.因此,Dictionary中的值必须实现IComparable接口. 尝试:下面的代码采用字典,获取KeyValuePair列表,对Value进行排序,然后返回字典中按其值报告的顶部键.private string[] getTopWords<T , U>(Dictionary<T, U> similarWordsAndWeights) {var myList = similarWordsAndWeights.ToList();myList.Sor...

c# – 具有明确类型安全分类的泛型类【代码】

我正在寻找一种方法来创建一个通用基类,它具有使用内部属性的类型安全分类.为了清楚起见,该类不必使用泛型语言功能,只要它是通用的,我正在寻找具有编译时类型安全性的东西. 作为一个例子,这里是一个简单的分类法,我想用同一个类的多个实例来表示WoodCrateBox MetalCrateBar其中的排列是Wood Crate Wood Box Metal Crate Metal Bar最初我虽然可以使用枚举来表示不同级别的分类法public enum EFirstLevel {Wood,Metal }public enum E...

c# – 带有where子句的泛型所需的显式强制转换【代码】

我希望有人可以建议一种方法来避免下面的“var o3”语句的显式转换.似乎编译器应该有足够的信息来隐式转换.using System.Collections.Generic;namespace Sample {public interface IPoint {double X { get; }double Y { get; }}public class Line<T> :List<T> where T:IPoint {}public class Graph<T> where T :IPoint {public Line<IPoint> Line1;public Line<T> Line2;public Graph() {var o1 = new Other(Line1); //worksvar o2...

在c#中使用泛型和Func避免代码重复的最佳方法【代码】

我想知道什么是避免使用Generics Func或任何其他方式重复某些recuring代码结构的最佳方法.作为一个实际的例子,我需要调用20个不同的WCF方法,但我希望有代码来处理异常. 假设这是wcf代理class ClassWithMethodsToCall // say wcf proxy{public Out1 GetOut1(In1 inParam) { return null; } // would have some spesific implementation public Out2 GetOut2(In2 inParam) { return null; }public Out3 GetOut3(In3 inParam) { retur...

c# – 如何使用泛型变量执行数学运算?【代码】

我正在开发一个需要整数字符串表示的项目,但是使用.NET Framework本身不支持的奇数数字库(据我所知) – 例如base36,62,64等.我们决定编写一个可以与任何数字库一起使用的整体字符串转换系统,因为它足够简单. 稍后,我们想要创建一个自定义的IFormatProvider / ICustomFormatter,以便在未来使用时更容易使用.但首先,我们希望以一些静态方法的形式解决转换过程本身,这些方法执行转换并返回一些基本结果.一旦我们开始工作,我们就会把IF...