generics

CreateDelegate with unknown types

∥☆過路亽.° 提交于 2020-02-10 09:01:06
问题 I am trying to create Delegate for reading/writing properties of unknown type of class at runtime. I have a generic class Main<T> and a method which looks like this: Delegate.CreateDelegate(typeof(Func<T, object>), get) where get is a MethodInfo of the property that should be read. The problem is that when the property returns int (I guess this happens for value types) the above code throws ArgumentException because the method cannot be bound. In case of string it works well. To solve the

CreateDelegate with unknown types

亡梦爱人 提交于 2020-02-10 08:58:10
问题 I am trying to create Delegate for reading/writing properties of unknown type of class at runtime. I have a generic class Main<T> and a method which looks like this: Delegate.CreateDelegate(typeof(Func<T, object>), get) where get is a MethodInfo of the property that should be read. The problem is that when the property returns int (I guess this happens for value types) the above code throws ArgumentException because the method cannot be bound. In case of string it works well. To solve the

Where are generic methods stored?

萝らか妹 提交于 2020-02-10 05:04:05
问题 I've read some information about generics in .ΝΕΤ and noticed one interesting thing. For example, if I have a generic class: class Foo<T> { public static int Counter; } Console.WriteLine(++Foo<int>.Counter); //1 Console.WriteLine(++Foo<string>.Counter); //1 Two classes Foo<int> and Foo<string> are different at runtime. But what about case when non-generic class having generic method? class Foo { public void Bar<T>() { } } It's obvious that there's only one Foo class. But what about method Bar

Swift protocol specializing generic protocol

流过昼夜 提交于 2020-02-08 07:34:12
问题 Is it possible to have a protocol that specializes a generic protocol? I want something like this: protocol Protocol: RawRepresentable { typealias RawValue = Int ... } This does compile, but when I try to access the init or rawValue from a Protocol instance, its type is RawValue instead of Int . 回答1: In Swift 4, you can add constraints to your protocol: protocol MyProtocol: RawRepresentable where RawValue == Int { } And now all methods defined on MyProtocol will have an Int rawValue. For

How to instantiate generic classes

好久不见. 提交于 2020-02-07 01:09:48
问题 I have an interface: public interface ICrawlService<T> where T : SocialPostBase { Task<int> Crawl(int accountId, Disguise disguise, ISocialAccountRepository socialAccountRepository, ISocialRepository<T> socialRepository, ISocialCrawlJobRepository jobRepository, IInstrumentationRepository instrumentationRepository); } My social repository is: public interface ISocialRepository<T> where T : class { IEnumerable<SocialPostCollection<T>> List { get; } Task Add(SocialPostCollection<T> entity,

getting an error on trying to call method in generic way

帅比萌擦擦* 提交于 2020-02-06 07:55:33
问题 Hi all I am trying to filter the table using where class by passing parameter to the method like as below, private IQueryable<SpaceFunctionType> Get<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class, ICategorySpaceFunction { return _dbContext.Set<TEntity>().Where(predicate) .Select(c => new SpaceFunctionType { Category = c.Category, SpaceFunction = c.SpaceFunction }); } public IQueryable<SpaceFunctionType> GetSpaceFunctionType(string environmentSource) { return Get

Build generic reusable iteration module from higher order function

百般思念 提交于 2020-02-06 04:29:10
问题 How can I create a reusable module for iteration which allows me to: choose whether it iterates in parallel lets me specify a higher-order function of which tasks need to be performed Here are some dummy functions. They for itself pose the second part of the question: Should I have an interface here that each of these functions at least implements the parameter inputDay:String ? or is it better to have a generic function with a configuration class, where the configuration implements a minimal

Invoke a method using reflection with the “params” keyword without arguments

旧城冷巷雨未停 提交于 2020-02-04 09:05:35
问题 Just like this question I'm having issues invoking a method that has the "params" keyword. I keep getting TargetParameterCountException exceptions. "Parameter count mismatch". The goal is to call this method with no parameters: IList<T> List(params Expression<Func<T, object>>[] includeProperties); Here is what I have so far: //Get generic type var entityType = typeof(Applicant).Assembly.GetType(string.Format("Permet.BackEnd.ETL.Domain.Models.{0}", tableName)); //create service that will

Invoke a method using reflection with the “params” keyword without arguments

被刻印的时光 ゝ 提交于 2020-02-04 09:04:54
问题 Just like this question I'm having issues invoking a method that has the "params" keyword. I keep getting TargetParameterCountException exceptions. "Parameter count mismatch". The goal is to call this method with no parameters: IList<T> List(params Expression<Func<T, object>>[] includeProperties); Here is what I have so far: //Get generic type var entityType = typeof(Applicant).Assembly.GetType(string.Format("Permet.BackEnd.ETL.Domain.Models.{0}", tableName)); //create service that will

Invoke a method using reflection with the “params” keyword without arguments

走远了吗. 提交于 2020-02-04 09:04:19
问题 Just like this question I'm having issues invoking a method that has the "params" keyword. I keep getting TargetParameterCountException exceptions. "Parameter count mismatch". The goal is to call this method with no parameters: IList<T> List(params Expression<Func<T, object>>[] includeProperties); Here is what I have so far: //Get generic type var entityType = typeof(Applicant).Assembly.GetType(string.Format("Permet.BackEnd.ETL.Domain.Models.{0}", tableName)); //create service that will