generics

Unable to constrain generic type

不羁岁月 提交于 2021-02-16 21:19:14
问题 I can't figure out what's happening here. I'm building a wrapper for a Dictionary collection. The idea is that, when the size of the collection is small, it will use a normal in-memory Dictionary; but, when a threshold number of items is reached, it will internally switch to an on-disk Dictionary (I'm using the ManagedEsent PersistentDictionary class). A snippet of the on-disk version is below. When compiling, it fails with the following error: "The type 'T_KEY' cannot be used as type

Unable to constrain generic type

萝らか妹 提交于 2021-02-16 21:17:08
问题 I can't figure out what's happening here. I'm building a wrapper for a Dictionary collection. The idea is that, when the size of the collection is small, it will use a normal in-memory Dictionary; but, when a threshold number of items is reached, it will internally switch to an on-disk Dictionary (I'm using the ManagedEsent PersistentDictionary class). A snippet of the on-disk version is below. When compiling, it fails with the following error: "The type 'T_KEY' cannot be used as type

Expression<Func<T, bool>> method argument

房东的猫 提交于 2021-02-16 20:09:16
问题 I'm trying to make a generic method that returns the string version of an expression: public string GetExpressionString(Expression<Func<T, bool>> expr) where T: class { return exp.Body.ToString(); } Cannot resolve symbol T Works well if I change T to a hard coded type. What did I miss? 回答1: You'll need to declare T as a generic type parameter on the method: public string GetExpressionString<T>(Expression<Func<T, bool>> exp) where T: class { return exp.Body.ToString(); } // call like this

Expression<Func<T, bool>> method argument

非 Y 不嫁゛ 提交于 2021-02-16 20:07:08
问题 I'm trying to make a generic method that returns the string version of an expression: public string GetExpressionString(Expression<Func<T, bool>> expr) where T: class { return exp.Body.ToString(); } Cannot resolve symbol T Works well if I change T to a hard coded type. What did I miss? 回答1: You'll need to declare T as a generic type parameter on the method: public string GetExpressionString<T>(Expression<Func<T, bool>> exp) where T: class { return exp.Body.ToString(); } // call like this

How to override generic method with derived type in c#

馋奶兔 提交于 2021-02-16 18:42:09
问题 I have the following classes: public interface IService { void ApplyChanges<T>(T parameters) where T : ParamBase; } public class ServiceBase : IService { public virtual void ApplyChanges<T>(T parameters) where T : ParamBase { } } public abstract class Service : ServiceBase { public override void ApplyChanges<T>(T parameters) where T : ParamBase { Console.WriteLine(parameters.Param2); //Apply changes logic here... } } public abstract class ParamBase { public string Param1 { get; set; } }

What do nested generics in C# mean?

杀马特。学长 韩版系。学妹 提交于 2021-02-16 17:00:55
问题 A bit of a basic question, but one that seems to stump me, nonetheless. Given a "nested generic": IEnumerable<KeyValuePair<TKey, TValue>> Is this stating that IEnumerable can have generic types that are themselves KeyValuePair 's ? Thanks, Scott 回答1: Yes. The KeyValuePair type expects two generic type parameters. We can either populate them by pointing to concrete types: IEnumerable<KeyValuePair<string, int>> Or we can populate them by using other generic parameters already specified by the

How to return concrete type from generic function?

天大地大妈咪最大 提交于 2021-02-16 15:29:06
问题 In the example below the Default trait is used just for demonstration purposes. My questions are: What is the difference between the declarations of f() and g() ? Why g() doesn't compile since it's identical to f() ? How can I return a concrete type out of a impl trait generically typed declaration? struct Something { } impl Default for Something { fn default() -> Self { Something{} } } // This compiles. pub fn f() -> impl Default { Something{} } // This doesn't. pub fn g<T: Default>() -> T {

Cast object to method generic type

我的梦境 提交于 2021-02-16 13:06:22
问题 This generates an error saying I cannot convert type ClassType to T . Is there any workaround for this? Is there any way to specify that the type of this can in fact be converted to T ? public void WorkWith<T>(Action<T> method) { method.Invoke((T)this); } 回答1: Two possible solutions: Not type-safe: public void WorkWith<T>(Action<T> method) { method.Invoke((T)(object)this); } This isn't typesafe because you can pass it any method that has a single parameter and no return value, like: WorkWith(

Cast object to method generic type

▼魔方 西西 提交于 2021-02-16 13:00:44
问题 This generates an error saying I cannot convert type ClassType to T . Is there any workaround for this? Is there any way to specify that the type of this can in fact be converted to T ? public void WorkWith<T>(Action<T> method) { method.Invoke((T)this); } 回答1: Two possible solutions: Not type-safe: public void WorkWith<T>(Action<T> method) { method.Invoke((T)(object)this); } This isn't typesafe because you can pass it any method that has a single parameter and no return value, like: WorkWith(

Cast object to method generic type

…衆ロ難τιáo~ 提交于 2021-02-16 13:00:36
问题 This generates an error saying I cannot convert type ClassType to T . Is there any workaround for this? Is there any way to specify that the type of this can in fact be converted to T ? public void WorkWith<T>(Action<T> method) { method.Invoke((T)this); } 回答1: Two possible solutions: Not type-safe: public void WorkWith<T>(Action<T> method) { method.Invoke((T)(object)this); } This isn't typesafe because you can pass it any method that has a single parameter and no return value, like: WorkWith(