Tag: generics

Prism – EventAggregator.GetEvent 。订阅() – 使用generics和约束

我有一个问题订阅事件Aggregator事件作为prism框架的一部分。 如果我使用像 eventAggregator.GetEvent().Subscribe(MyMethod) 然后一切正常,我的方法在事件发布时触发。 但是,当移动到除I string之外的更复杂的对象时,我遇到了问题。 我有一堆类,它们都来自一个接口(IRequest),例如 我的事件类设置如下 public class MyEvent : PubSubEvent<MyClass> where TRequest : IRequest {} 我有一个generics类(MyClass)在其中使用IRequest的对象 – 再次,在这一点上似乎一切正常。 现在,假设我发布了一个使用其中的对象Profile的MyClass事件: eventAggregator.GetEvent<MyEvent>().Publish(myProfileObject); 在我的订阅中,我想要一个可以捕获MyEvent的所有事件的方法 – 与T是Profile还是从IRequest派生的其他对象无关 – 这是我似乎遇到问题的地方。 在下面的例子中,第一个工作,但第二个没有 – 我理想的是喜欢使用类似于第二个的东西。 eventAggregator.GetEvent<MyEvent>().Subscribe(test1); void test1 (MyClass obj) { //Some stuff here } eventAggregator.GetEvent<MyEvent>().Subscribe(test2); void test2(MyClass obj) where T : IRequest { //never gets called } 我的假设是因为Profile来自IRequest然后它应该工作??? […]

使用Generics返回文字字符串或从Dictionary

我想我这次出局了。 随意编辑标题我也想不出一个好的标题。 我正在读取一个文件然后在该文件中将是一个字符串,因为它像一个xml文件。 但是在文件中将是一个文字值或“命令”来从workContainer获取值 所以 me@company.com 要么 [? MyEmail ?] 我想做什么,而不是写在各处,把它放在一个通用的function 所以逻辑是 If Container command grab from container else grab string and convert to desired type Its up to the user to ensure the file is ok and the type is correct 另一个例子是 所以 3 要么 [? NumberOfSales ?] 这是我开始研究的程序 public class WorkContainer:Dictionary { public […]

为什么ForEach扩展方法在C#中的ObservableCollections中不可用

可能重复: 为什么IEnumerable接口上没有ForEach扩展方法? 为什么在ObservableCollections类中可用的ForEach扩展方法在List中可用?

将generic.list转换为ArrayOfInt,以便通过SOAP传输到Web服务

我正在尝试使用SOAP协议将一个通用的整数列表从客户端应用程序传递给Web服务。 当我尝试将列表作为参数传递给Web服务中声明的Web方法时,我收到错误“无法从generic.list转换为ArrayOfInt”。 我该如何解决这个问题? // web service method [WebMethod(CacheDuration = 30, Description = “Returns the calculated sum value of all numbers supplied in the list”)] public int CalculateListSum(int[] list) { int _sum = 0; foreach (int _val in list) { _sum += _val; } return _sum; } // client app buton click event private void btnRun_Click(object […]

具有新类型约束的通用构造函数

我有两种类型的对象,数据库模型和普通系统模型。 我希望能够将模型转换为数据库模型,反之亦然。 我写了以下方法: public static E FromModel(T other) where T : sysModel where E : dbModel { return new E(other); } 基本上sysModel和dbModel都是抽象的。 dbModel有许多inherting类,都有复制构造函数。 我收到: 无法创建类型参数“E”的实例,因为它没有new()约束 我知道技术上有时我没有为T每个值都有匹配的构造函数,至少调试器知道什么。 我也尝试添加where E : dbModel, new()约束,但它只是无关紧要。 有没有办法使用generics方法和使用参数将模型转换为另一个模型? 谢谢。

当参数是通用的时,Type.GetMethod的Type数组中应该包含哪些类型?

如果我想通过reflection调用generics方法,我可以很容易地使用这种技术,除非: 该方法只能通过其参数区分。 该方法有一个参数,其类型是方法的类型参数之一。 调用Type.GetMethod(string, Type[])时,如何在Type[]数组中指定generics参数? 例: public class Example { //This is the one I want to call. public void DoSomething(T t) { … } public void DoSomething(Foo foo) { … } public void CallDoSomething(Type type, object value) { MethodInfo method = typeof(Example) .GetMethod(“DoSomething”, new Type[] {/* what do i put here? */ }); MethodInfo […]

使用Json.Net反序列化时,在运行时将System.Type用作

我有一个进程需要能够以下面的方式调用函数。 public static Task Convert(string payload, Type type) { JsonSerializerSettings settings = new JsonSerializerSettings().Configure();//<— Pull in extension methods to handle custom types return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(payload), settings)); } 上面的代码不能编译。 接下来,我使用John Skeet对2011年类似于我的问题的回答 。 public static string Convert(string payload, Type type) { JsonSerializerSettings settings = new JsonSerializerSettings().Configure(); //This clashes with another overload of DeserializeObject in the method table MethodInfo […]

如何在generics方法调用中使用Type变量(C#)

我将一个Type变量传递给一个方法,我想在调用IQueryable.Join时将其用作通用参数,如下所示 queryResult.Join( items, outerSelector, innerSelector, ( a, b) => a); 它显然不起作用。 为了达到我的目的,我该怎么做? 谢谢!

嵌套接口:将IDictionary <TKey,IList >转换为IDictionary <TKey,IEnumerable >?

我认为将IDictionary<TKey, IList>对象转换为IDictionary<TKey, IEnumerable>是相当简单的,但是 var val = (IDictionary<TKey, IEnumerable>)Value; 抛出System.InvalidCastException ,和 var val = Value as IDictionary<TKey, IEnumerable>; 使val null。 投这个的正确方法是什么?

方法参数接受多种类型

我正在开发一个应用程序,在我有多种类型的RichTextBox我已经定制(RichTextBox,RichAlexBox,TransparentRichTextBox) 。 我想创建一个方法来接受所有这些类型加上一些其他参数。 private void ChangeFontStyle(RichTextBox,RichAlexBox,TransparentRichTextBox rch, FontStyle style, bool add) { //Doing somthing with rch.Rtf } 我通过StackOverFlow搜索并找到了一些这样的答案,我无法弄清楚如何使用它们来解决我的问题 void foo() //There’s just one parameter here where TOne : BaseOne //and I can’t figure out how to define my other two parameters where TTwo : BaseTwo 我也试过重载,因为这个答案提供, private void ChangeFontStyle(TransparentRichTextBox rch, FontStyle style, bool add); private […]