generic-method

Moq It.IsAnyType not working for Func returning Task with generic type

微笑、不失礼 提交于 2021-02-10 07:53:51
问题 I've tried 3 different ways to setup a mock for a generic interface method. Only one way works, but it is using an explicit type, so won't work generically. I tried using It.IsAnyType, but it doesn't seem to match on the call that is made. Here is the sample code (I expected test case 1 to have "asdf" returned). How can I get the mock to work for any type (not just string?)? using Moq; using System; using System.Threading.Tasks; namespace blah { public class Junk : IGetOrAddable { public

MOQ- Setting up and verifying a generic method with Func argument

邮差的信 提交于 2021-01-28 19:34:21
问题 I have a third party interface which I want to mock its methods. To make my purpose clear consider the following IFoo interface which has a generic method like M2. One of M2 arguments is of type Func. public interface IFoo { bool M1<T>(); bool M2<T>(T arg, Func<T, string> func); } If I set up the M2 method as: var mock = new Mock<IFoo>(); mock.Setup(foo => foo.M2(It.IsAny<It.IsAnyType>(),It.IsAny<Func<It.IsAnyType, string>>())).Returns(true); mock.Object.M2("arg1", s => s); mock.Verify(foo =>

What does <T> before the return type of a method mean?

南笙酒味 提交于 2021-01-28 11:15:17
问题 The following method returns a List composed of T type elements: public <T> List<T> getList() { return new ArrayList<T>(); } In the signature we have <T> List<T> . The List<T> makes sense, because that is the type of the return value . What is the need for the preceding <T> though? The code doesn't compile without both <T> and List<T> . Leaving out the <T> gives Cannot resolve symbol T I have read the official Oracle tutorial on generic methods. It explains that this is part of the syntax:

Dart: doesn't override generic method has generic parameter type similar to the return type

这一生的挚爱 提交于 2020-07-22 21:39:15
问题 I have an issue to override a generic method from an abstract class. Here is my abstract class: abstract class A { String getData<Type>(Type key); } when I created a class (B) to implement class (A) as showing below: class B implements A { @override String getData<String>(String key) { //Error <= B.getData' ('String Function<String>(String)') isn't a valid override of 'A.getData' ('String Function<Type>(Type)') return ""; } } Shown the below compilation error at (getData) method: 'B.getData'

Dart: doesn't override generic method has generic parameter type similar to the return type

拈花ヽ惹草 提交于 2020-07-22 21:37:45
问题 I have an issue to override a generic method from an abstract class. Here is my abstract class: abstract class A { String getData<Type>(Type key); } when I created a class (B) to implement class (A) as showing below: class B implements A { @override String getData<String>(String key) { //Error <= B.getData' ('String Function<String>(String)') isn't a valid override of 'A.getData' ('String Function<Type>(Type)') return ""; } } Shown the below compilation error at (getData) method: 'B.getData'

Dart: doesn't override generic method has generic parameter type similar to the return type

被刻印的时光 ゝ 提交于 2020-07-22 21:37:38
问题 I have an issue to override a generic method from an abstract class. Here is my abstract class: abstract class A { String getData<Type>(Type key); } when I created a class (B) to implement class (A) as showing below: class B implements A { @override String getData<String>(String key) { //Error <= B.getData' ('String Function<String>(String)') isn't a valid override of 'A.getData' ('String Function<Type>(Type)') return ""; } } Shown the below compilation error at (getData) method: 'B.getData'

How to create a generic method in Dart?

做~自己de王妃 提交于 2020-05-12 19:14:02
问题 I'm trying to use generic methods in Dart (1.22.0-dev.10.3). Here is a simple example: abstract class VR<T> { VR(); bool foo<T>(T value); } class VRInt extends VR<int> { VRInt(); bool foo<int>(int n) => n > 0; // Thinks n is Object } class VRString extends VR<String> { VRString(); bool foo<String>(String s) => s.length > 0; // Thinks s is Object } Both subclasses generate errors that say the argument to foo is an Object. I'm sure this is just a syntactic error on my part, but I've searched

How to create a generic method in Dart?

▼魔方 西西 提交于 2020-05-12 19:10:36
问题 I'm trying to use generic methods in Dart (1.22.0-dev.10.3). Here is a simple example: abstract class VR<T> { VR(); bool foo<T>(T value); } class VRInt extends VR<int> { VRInt(); bool foo<int>(int n) => n > 0; // Thinks n is Object } class VRString extends VR<String> { VRString(); bool foo<String>(String s) => s.length > 0; // Thinks s is Object } Both subclasses generate errors that say the argument to foo is an Object. I'm sure this is just a syntactic error on my part, but I've searched

How to get MethodInfo of a generic method on a non generic .NET type? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 08:50:12
问题 This question already has answers here : Is there a good way of getting MethodInfo of open generic method? (3 answers) Closed 6 years ago . I have this little problem, that I cannot figure out which arguments to pass to Type.GetMethod in order to get back the MethodInfo of a generic method on a non generic type. Specifically, I have this type definition: public static class A { public static B F<T>(bool dummy) { } public static B F<T>(IEnumerable<T> arg) { ... } } I have tried several takes

Use Reflection to call generic method on object instance with signature: SomeObject.SomeGenericInstanceMethod<T>(T argument)

元气小坏坏 提交于 2019-12-28 07:03:11
问题 How do I call SomeObject.SomeGenericInstanceMethod<T>(T arg) ? There are a few posts about calling generic methods, but not quite like this one. The problem is that the method argument parameter is constrained to the generic parameter. I know that if the signature were instead SomeObject.SomeGenericInstanceMethod<T>(string arg) then I could get the MethodInfo with typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter)