Tag: generics

C#generics usercontrol

我想定义以下控件: public partial class ObjectSelectorControl : UserControl where T : class 问题是设计师无法解决这个问题。 这个问题有解决方法吗?

ObservableCollection:使用多个新项调用OnCollectionChanged

请注意,我正在尝试使用NotifyCollectionChangedAction.Add操作而不是.Reset。 后者确实有效,但对于大型集合来说效率不高。 所以我inheritance了ObservableCollection: public class SuspendableObservableCollection : ObservableCollection 出于某种原因,这段代码: private List _cachedItems; … public void FlushCache() { if (_cachedItems.Count > 0) { foreach (var item in _cachedItems) Items.Add(item); OnCollectionChanged(new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Add, (IList)_cachedItems)); } } 抛出一个集合添加事件是指不属于集合的项目 这似乎是BCL的一个错误? 我可以在调用OnCollectionChanged之前逐步查看将新项添加到this.Items 哇 刚刚发现了一个惊人的发现。 这些方法都不适合我(flush,addrange),因为只有当这个集合绑定到我的Listview时才会触发错误! TestObservableCollection testCollection = new TestObservableCollection(); List testTrades = new List(); for (int i = 0; […]

C#:使用generics来创建指针数组

下午所有, 如果你愿意,请帮助一下。 为了规避.NET中的2Gb对象限制,我创建了一个在堆上分配内存的类,这允许我创建最多可达RAM空间的数组。 然而,为了便于开发(因为它是一个概念certificate),它很难编码为多头。 现在它工作了我一直在尝试改变代码使用generics,所以我可以使用相同的代码多种类型。 在分配内存并正确索引数组时,我需要一个与数组相同类型的指针数组,即长数组需要long*[] myLargeArray 。 问题是当我使用generics时,这个声明变成T*[] myLargeArray ,它总是产生错误‘不能获取地址,获取大小,或者声明指向托管类型的指针(’T’)’ 提前致谢。 PS在有人问之前,是的,我确实需要这么大的arrays。 2D数组的代码示例: LargeArray myArray = new LargeArray(x, y); public unsafe class LargeArray where T : struct { … private T*[] tArr; … public LargeArray(long sizeI, long sizeJ) { … myLargeArray = new T*[sizeI]; … } }

从C#中的通用对象获取属性

请看一下这段代码: public void BindElements(IEnumerable dataObjects) { Paragraph para = new Paragraph(); foreach (T item in dataObjects) { InlineUIContainer uiContainer = this.CreateElementContainer(item.FirstName ????? ) para.Inlines.Add(uiContainer); } FlowDocument flowDoc = new FlowDocument(para); this.Document = flowDoc; } 在Visual Studio“item.XXX”中编写时,我应该从我的权限中获取属性,如.FirstName或.LastName。 我不知道数据对象是IEnumerable还是IOrder等……它必须是通用的! 如何获得真实属性表单项? 只有反思?

实现IList接口

我是仿制药的新手。 我想通过从IList接口派生它来实现我自己的集合。 你能给我一些实现IList接口的类的链接,或者为我提供一个至少实现Add和Remove方法的代码吗?

字符串值的通用类型

我有一个自定义类,它依赖于传递类型T来传递。 我只知道字符串forms是什么类型,因为它是如何被发送的。 我一直在寻找,但似乎无法找到我需要的确切内容。 我可以将字符串值解析为一个类型,但我需要将其解析为…某些东西,我可以作为generics参数传递。 我已经改写了我的问题,如下: // Classes structure namespace Mynamespace { public interface IRequest { } public interface IHandler where T : IRequest { void Handle(T item); } public class MyRequest : IRequest { } public class MyHandler : IHandler { void Handle(MyRequest item) { } } } // The info I get, and I […]

AutoFixture:配置Open Generics Specimen Builder

我有一个使用Open Generics的对象模型(是的,是的,现在我有两个问题;这就是为什么我在这里:): – public interface IOGF { } class C { } class D { readonly IOGF _ogf; public D( IOGF ogf ) { _ogf = ogf; } } 我正在尝试让AutoFixture生成上面的D匿名实例。 然而,就其本身而言,AutoFixture没有构建IOGF的内置策略,因此我们观察到: public class OpenGenericsBinderDemo { [Fact] public void X() { var fixture = new Fixture(); Assert.Throws( () => fixture.CreateAnonymous() ); } 基本信息是: Ploeh.AutoFixture.ObjectCreationException:AutoFixture无法从IOGF`1 [C]创建实例,很可能是因为它没有公共构造函数,是抽象或非公共类型。 我很高兴为它提供一个具体的实现: […]

通用结构的大小

我需要找出一个通用结构的大小(我不能像sizeof(T)或使用Marshal.SizeOf(…)0>给我一个错误) 所以我写道: public static class HelperMethods { static HelperMethods() { SizeOfType = createSizeOfFunc(); } public static int SizeOf() { return SizeOfType(typeof(T)); } public static readonly Func SizeOfType = null; private static Func createSizeOfFunc() { var dm = new DynamicMethod(“SizeOfType”, typeof(int), new Type[] { typeof(Type) }); ILGenerator il = dm.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Sizeof); //needs to be il.Emit(OpCodes.Sizeof, […]

具有通用服务的服务结构

我希望有一个通用类型的服务,即 – public interface IFooService { Task Get(int id); } 但是,服务结构不允许generics类或generics方法。 我也尝试过类似的东西 public interface INinjaService : IService, IFooService { } 但它没有选择inheritance的接口说明 服务类型“Omni.Fabric.Services.NinjaService”未实现任何服务接口。 服务接口是从“Microsoft.ServiceFabric.Services.Remoting.IService”类型派生的接口。 我似乎无法在Service Fabric文档或stackoverflow上找到任何关于generics的引用。 要么它仍然太新,要么我可能走错了路。 有没有人有幸实现这种模式? 可以吗? 它应该完成吗? NinjaService按要求提供 public class NinjaService : StatelessService, INinjaService { public NinjaService(StatelessServiceContext serviceContext) : base(serviceContext) { } protected override IEnumerable CreateServiceInstanceListeners() { return new[] { new ServiceInstanceListener(context => […]

从基类方法返回派生类型

我有一个类似于这个的类层次结构: public class Base { private List attributes = new List(); public T WithAttributes(params string[] attributes) where T : Base { this.attributes.AddRange(attributes); return this as T; } } public class Derived : Base { } 我想以fluent-api样式语法从派生类调用Base.WithAttributes并返回派生实例,如下例所示。 void Main() { Derived d = new Derived(); // CS0411 The type arguments for method ‘UserQuery.Base.WithAttributes(params string[])’ cannot be […]