编辑:
当然,我的真实代码并不完全像下面这样。我试着写半伪代码,让它更清楚我想要做什么。
看起来只是把事情搞砸了。
所以,我真正想做的是:
Method<Interface1>();
Method<Interface2>();
Method<Interface3>();
...
好吧..。我想也许我可以用反射把它变成一个循环。所以问题是:我该怎么做呢?我对反射的了解很浅。因此,代码示例将是很棒的。
场景看起来像这样:
public void Method<T>() where T : class
{}
public void AnotherMet
看这张截图
它显示所有定义的类型都是RuntimeType,然后我使用myType.IsAssignableFrom(definedType),它从不返回true。
这是可以理解的,因为它是RuntimeType,但是如何从它获得实际类型。
public static List<T> GetAllDerivedTypes<T>(this Assembly assembly)
{
var type = typeof(T);
var isInterface = type.IsInterface;
var name = type.Name;
我知道这方面已经有几个问题了,但我似乎无法做到这一点。
我有这样一门课;
public class TopLocation<T> : ILocation
{
public string name { get; set; }
public string address { get; set; }
}
在创建类时,我指定它是IRestaurant还是IClub。到目前为止没问题。
但是,如何测试以确定它是IClub还是IRestaurant在if语句中?
这是失败的;
if (item.trendItem is ILocation<ITopRestaurant&g
我试图从json.net迁移到微软的json,并发现一些行为非常不同的东西。
让我们使用这个简化的示例:
public interface IName
{
string Name { get; set; }
}
public class Person : IName
{
public string Name { get; set; }
public int Age { get; set; }
}
public void Foo()
{
IName p = new Person {Age = 4, Name = "Waldo"};
va
我有几个从abstract class BrowsingGoal继承的类。其中一些实现了一个名为ICanHandleIntent<TPageIntent> where TPageIntent: PageIntent的接口。
举一个具体的例子:
public class Authenticate : BrowsingGoal, ICanHandleIntent<AuthenticationNeededIntent>
{
...
}
现在,我想扫描CurrentDomain的程序集,以查找使用ICanHandleIntent实现AuthenticationNeede
可能重复:
目的:动态加载"MyContent“类。我有一个接口<T>,一个抽象通用class<T>。
代码:
public interface IMyObjectInterface{
}
public abstract MyAbstractObject : IMyObjectInterface{
}
public class MyObject : MyAbstractObject{
}
public interface IMyContentInterface<T> where T : MyAbstractObject
{
T MyMe
我正在使用反射创建泛型类型的实例:
public interface IModelBuilder<TModel>
{
TModel BuildModel();
}
public class MyModel
{
public string Name { get; set; }
}
public class MyModelBuilder : IModelBuilder<MyModel>
{
public MyModel BuildModel()
{
throw new NotImplementedException();
我正在尝试以一种未来的方式编写我的合成根,这样当我添加更多使用SimpleInjector 4实现基本实体的命令时,它们会自动被拾取。
域类如下所示:
public interface ICommandHandler<TCommand> { . . . }
public class UpdateCommandHandler<T> : ICommandHandler<UpdateCommand<T>>
where T : EntityBase { . . . }
public class UpdateCommand<T>
在运行时比较两种类型最安全的方法是什么?
public interface IHandler<T> where T : Command {
}
public class CleanupHandler : IHandler<CleanupCommand> {
}
var Handlers = GetServices(typeof(IHandler<Cleanup>));
static IEnumerable<object> GetServices(Type serviceType) {
var services=
如何动态加载"MyContent“类?我有一个interface<T>,一个抽象泛型class<T>和一个类。检查我的代码:
public interface IMyObjectInterface{
}
public abstract MyAbstractObject : IMyObjectInterface{
}
public class MyObject : MyAbstractObject{
}
public interface IMyContentInterface<T> where T : MyAbstractObject
{
vo
我正在尝试创建一个从其他地方检索的特定类型的泛型列表:
Type listType; // Passed in to function, could be anything
var list = _service.GetAll<listType>();
然而,我得到一个构建错误:
The type or namespace name 'listType' could not be found (are you missing a using directive or an assembly reference?)
这是可能的吗?或者我正在涉足C# 4的动态领域?
作
Bellow是我的代码的简化版本:
public interface IControl<T>
{
T Value { get; }
}
public class BoolControl : IControl<bool>
{
public bool Value
{
get { return true; }
}
}
public class StringControl : IControl<string>
{
public string Value
{
get { return
我需要得到从抽象泛型类派生出来的所有类型。我还需要获得泛型类型(如实现泛型接口的类型上的GetGenericArguments() )。
这是示例代码:
public abstract class AbstractValidator<T>
public class CreateTripValidator : AbstractValidator<CreateTrip>
public class CancelTripValidator : AbstractValidator<CancelTrip>
我希望加载从AbstractValidator派生的所有类型
我有一个接口IDigitalState,定义为
Public Interface IDigitalState
ReadOnly Property Code As Integer
ReadOnly Property Name As String
End Interface
和一个实现此接口的结构
Public Structure DigitalState
Implements IDigitalState
Private ReadOnly mCode As Integer
Private ReadOnly mName As String
Pu
我正在尝试获取从IService派生的所有接口(它们都是开放的通用接口),这些接口都是Random类实现的。我确信至少有一个接口iServiceTypeInterfaces正在实现来自IService<,> 的继承,但没有将任何项添加到 list中。
我有以下代码:
var iServiceTypeInterfaces = new List<Type>();
Type iServiceGeneric = typeof(IService<,>);
foreach(Type i of Random.GetInterfaces())
{
Type curren
在我的程序集中,我有很多常规的、封闭的和打开的类型。我有一个疑问,我试图从它中排除打开的类型
class Foo { } // a regular type
class Bar<T, U> { } // an open type
class Moo : Bar<int, string> { } // a closed type
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???);
types.Foreach(t => ConsoleWriteLine(t.Name))
当使用ASP.NET MVC +实体框架并尝试实现泛型存储库和泛型服务时,由Unity解决所有问题:
我试图让Unity使用参数注入将一个通用服务注入到控制器中,但是类型解析失败了,这条错误消息如下:
在试图获取类型KeyMyApp.Services.Implementation.SupplierService,实例时发生激活错误当前生成操作(生成键生成ISupplierService为null)失败:试图获取IGenericRepository1, key \"\" Resolution of the dependency failed: The current t
我想使用一个使用MEF的队列插件。我尝试过下面的方法,但是下面用导入属性修饰的字段queuePlugins和alternativeApproach (在下面的例子中)都是空的,你知道吗?谢谢,ATom
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
public class P { }
public class B : P { }
public class A : P { }
public interface Interface<T> where T : P { }
public class IA : Interface<A> { }
public class IB : Interface<B> { }
public class Test
{
public void WhatTheFuck()
{
Interface<P> p;
p = n
我有以下几个类:
// -- model hierarchy
public interface IJob {
}
public abstract class AbstractJob : IJob {
}
public class FullTimeJob : AbstractJob {
}
// -- dao hierarchy
public interface IJobDao<T> where T : IJob {
T findById(long jobId);
long insert(T job);
}
public i
我有一个现有的程序,它有一些插件基础设施,目前依赖于具有无参数构造函数的插件类。我想为插件作者提供一个机会,通过为构造函数指定参数来从我的程序中只需要一些基础设施。在内部,我使用一些通用包装类来封装插件的类,并使它们像以前的插件时代的内部类一样在我的程序的其余部分运行。
这里有一些占位符表示我的基础设施:
public interface IInfrastructure
{
}
public class Infrastructure : IInfrastructure
{
}
一些插件接口规范:
public interface IPlugi
我有一个简单的工厂方法,它根据提供的泛型类型参数提供一个具体的实现实例。如果具体类继承自带有类型参数的公共抽象基类,则不能强制转换它们。编译器告诉我Error 2 Cannot convert type 'Car' to 'VehicleBase<T>'。如果将抽象类替换为具有相同类型参数的接口,或者从抽象类中删除泛型类型参数,则可以很好地工作。
interface IWheel
{
}
class CarWheel : IWheel
{
}
abstract class VehicleBase<T>
{
}
class C
我实现了一个通用存储库,但需要在运行时动态创建适当的存储库,并将实体类型指定为字符串。
我已经设法使用以下命令创建了存储库:
Assembly common = Assembly.LoadFrom(@"CommonLibrary.dll");
Type entityType = common.GetType("Models.OneOfManyEntities");
Type repoType = typeof(TradeSiftRepository<>).MakeGenericType(entityTy
我正在尝试构建映射配置文件的层次结构,以简化为符合该层次结构的新对象创建映射。但我很难找到一些映射来工作。我使用泛型类型参数添加的映射在目标类型时似乎不起作用,但当它是源类型时则工作。
考虑以下类型:
public interface IRequest
{
string Input { get; }
}
public interface IResponse
{
string Output { get; }
}
public class SomeObject
{
public string Value { get; set; }
}
abstract class Base
我们有一组组件,每个组件都应该以自己的方式加载。
请考虑以下几点:
public interface IComponent
{
Type LoaderType {get;}
}
public class ComponentA: IComponent
{
Type LoaderType => typeof(ComponentALoader);
}
public class ComponentB: IComponent
{
Type LoaderType => typeof(ComponentBLoader);
}
public interface ILo
我被泛型和一些建议搞得焦头烂额。假设我有以下接口:
public interface IThing<T>
{
string DoStuff(T input);
}
以及以下实现:
public class GenericThing<T> : IThing<T> where T : Person
{
public string DoStuff(T input)
{
return typeof(T).Name;
}
}
和
public class GenericThing2<T> : IThing<