generics

Unable to convert List<List<int>> to return type IList<IList<int>>

▼魔方 西西 提交于 2020-08-19 11:41:56
问题 For level order traversal why does this exception occur? Following exception occurs: Cannot implicitly convert type ' System.Collections.Generic.List<System.Collections.Generic.List<int>> ' to ' System.Collections.Generic.IList<System.Collections.Generic.IList<int>> '. An explicit conversion exists (are you missing a cast?) public IList<IList<int>> LevelOrder(TreeNode root) { var result = new List<List<int>>(); var que = new Queue<TreeNode>(); //if(root==null) return result; que.Enqueue(root)

Unable to convert List<List<int>> to return type IList<IList<int>>

♀尐吖头ヾ 提交于 2020-08-19 11:41:50
问题 For level order traversal why does this exception occur? Following exception occurs: Cannot implicitly convert type ' System.Collections.Generic.List<System.Collections.Generic.List<int>> ' to ' System.Collections.Generic.IList<System.Collections.Generic.IList<int>> '. An explicit conversion exists (are you missing a cast?) public IList<IList<int>> LevelOrder(TreeNode root) { var result = new List<List<int>>(); var que = new Queue<TreeNode>(); //if(root==null) return result; que.Enqueue(root)

Making the inner class of a generic class extend Throwable [duplicate]

谁说我不能喝 提交于 2020-08-19 11:31:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why doesn’t Java allow generic subclasses of Throwable? I'm trying to make a regular RuntimeException inside a generic class like this: public class SomeGenericClass<SomeType> { public class SomeInternalException extends RuntimeException { [...] } [...] } This piece of code gives me an error on the word RuntimeException saying The generic class SomeGenericClass<SomeType>.SomeInternalException may not subclass

How to access derived class members from an interface?

流过昼夜 提交于 2020-08-19 05:43:36
问题 I have three classes; Stamp, Letter and Parcel that implement an interface IProduct and they also have some of their own functionality. public interface IProduct { string Name { get; } int Quantity { get; set; } float Amount { get; } } public class Stamp : IProduct { public string Name { get { return "Stamp"; } } public int Quantity { get; set; } public float Amount { get; set; } public float UnitPrice { get; set; } } public class Letter : IProduct { public string Name { get { return "Letter"

How to access derived class members from an interface?

时光怂恿深爱的人放手 提交于 2020-08-19 05:42:45
问题 I have three classes; Stamp, Letter and Parcel that implement an interface IProduct and they also have some of their own functionality. public interface IProduct { string Name { get; } int Quantity { get; set; } float Amount { get; } } public class Stamp : IProduct { public string Name { get { return "Stamp"; } } public int Quantity { get; set; } public float Amount { get; set; } public float UnitPrice { get; set; } } public class Letter : IProduct { public string Name { get { return "Letter"

Creating an Array of unknown type

对着背影说爱祢 提交于 2020-08-19 04:32:33
问题 I have an object which I must validate values off the problem, some of the attributes of the Objects are arrays of custom objects. Such that it will involve some boring down into the individual elements of the array. Excuting the getters for each element such as: AttribGrp[] x = Object.getAttribGrp() x[i].getSomeValue() It is this I need to get to. I have been extracted the data using an Enum with the list of the attributes In the following manner. public String getAttribValueAsString

Check if object is of non-specific generic type in C#

大兔子大兔子 提交于 2020-08-18 05:35:17
问题 Say I have the following class: public class General<T> { } And I want to find out if an object is of that type. I know I can use reflection to find out whether the object is of that generic type with Type.GetGenericTypeDefinition , but I want to avoid that. Is it possible to do something like obj is General<T> , or obj.GetType().IsAssignableFrom(typeof(General<T>)) ? I'm quite surprised that I couldn't find a similar question, although I may have used wrong keywords in my searches. 回答1: You

Check if object is of non-specific generic type in C#

巧了我就是萌 提交于 2020-08-18 05:35:09
问题 Say I have the following class: public class General<T> { } And I want to find out if an object is of that type. I know I can use reflection to find out whether the object is of that generic type with Type.GetGenericTypeDefinition , but I want to avoid that. Is it possible to do something like obj is General<T> , or obj.GetType().IsAssignableFrom(typeof(General<T>)) ? I'm quite surprised that I couldn't find a similar question, although I may have used wrong keywords in my searches. 回答1: You

How to resolve a generic type service from the DependencyInjection at Asp.net Core runtime

狂风中的少年 提交于 2020-08-11 04:51:34
问题 I am dealing with a WeChat project that post XML message to my server. The message could be any one of several types. Hence I first deserialize the message to corresponding object with a base "WxMessage", and then return the object to a dispatcher, which will find out a correct message handler to handle the message. The handlers, corresponding to each type of messages, were registered to the DependencyInjection of Asp.net core 2.1 with a IWxMessageHandler<> interface. services.AddScoped

Comparing two lists and removing each item that contains an entry from the other list

半世苍凉 提交于 2020-08-10 19:06:17
问题 I'm trying to essentially copy any list items in fileManifest but only those that don't contain in any of the items from exclusionFilters to a newly initialized list. I haven't figured out an elegant way to do this other than a nested foreach loop. Does someone by chance have a better solution for this problem? Maybe LINQ ? var fileManifest = new List<string>() { @"C:\Test\Directory1\File1.xml", @"C:\Test\Directory1\File2.xml", @"C:\Test\Directory1\Directory2\File1.xml", }; var