我正在尝试使用类似于此的代码扫描程序集以查找实现特定接口的类型:
public List<Type> FindTypesImplementing<T>(string assemblyPath)
{
var matchingTypes = new List<Type>();
var asm = Assembly.LoadFrom(assemblyPath);
foreach (var t in asm.GetTypes())
{
if (typeof(T).IsAssignableFrom(t))
matchingTypes.Add(t);
}
return matchingTypes;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在某些情况下,我得到一个ReflectionTypeLoadException调用asm.GetTypes(),例如,如果程序集包含引用当前不可用的程序集的类型.
就我而言,我对导致问题的类型不感兴趣.我正在搜索的类型不需要不可用的程序集.
问题是:是否有可能以某种方式跳过/忽略导致异常但仍处理程序集中包含的其他类型的类型?
在给定的命名空间下,我有一组实现接口的类.我们称之为ISomething.我有另一个类(让我们称它CClass),它知道ISomething但不知道实现该接口的类.
我希望CClass找到所有的实现ISomething,实例化它的实例并执行该方法.
有没有人知道如何用C#3.5做到这一点?
是否有更好的(更高性能或更好的代码;)方法来查找类型的所有派生类型?目前即时使用以下内容:
我想知道是否有更好的方式来做这件事?
对于一个对象,我可以使用反射获取所有子类吗?
如何获得实现特定开放泛型类型的所有类型?
例如:
public interface IUserRepository : IRepository<User>
Run Code Online (Sandbox Code Playgroud)
找到所有实现的类型IRepository<>.
public static IEnumerable<Type> GetAllTypesImplementingOpenGenericType(Type openGenericType, Assembly assembly)
{
...
}
Run Code Online (Sandbox Code Playgroud) 只是想知道两者之间是否存在任何差异,在完全信任的asp.net mvc 2应用程序的环境中.
与获取实现接口的所有类型相关,我们可以轻松地获得实现特定接口的Assembly中的所有类型.
例:
interface IFace
{
}
class Face : IFace
{
}
class TwoFace : Face
{
}
Run Code Online (Sandbox Code Playgroud)
对于这个结构,我们将通过反射找到这两个类,即使用第一个实现派生的所有类
GetTypes().Where(
type => type.GetInterfaces().Contains(typeof(IFace))
)
Run Code Online (Sandbox Code Playgroud)
所以问题是:如何将结果限制为最初实现接口的基类?! 在此示例中:只有类类型Face是相关的.
我是状态设计模式的新手,我找不到将对象的不同状态保存到数据库的正确示例(在我的例子中是SQL Server).该场景与下一篇文章中描述的示例非常相似[几乎相同],但是我没有找到将状态持久保存到数据库的适用解决方案.你们可以推荐一个链接或者可能举个例子吗?
另外:如何在运行时枚举所有不同的ConcreteState类型?举例来说,如果你有10个不同的国家,你声明与10个不同成员的EnumStates,并给每一个成员的ConcreteState关联EnumStates成员,或者你通过得到的ConcreteState的子类得到所有的不同的状态?
为了您的信息,我需要能够根据不同的状态搜索实体.
我想创建一个方法,该方法返回一个类型(或 IEnumerable 类型),该类型实现一个采用类型参数的特定接口——但是我想通过该泛型类型参数本身进行搜索。作为示例,这更容易演示:
我想要的方法签名:
public IEnumerable<Type> GetByInterfaceAndGeneric(Type interfaceWithParam, Type specificTypeParameter)
Run Code Online (Sandbox Code Playgroud)
然后如果我有以下对象
public interface IRepository<T> { };
public class FooRepo : IRepository<Foo> { };
public class DifferentFooRepo : IRepository<Foo> {};
Run Code Online (Sandbox Code Playgroud)
然后我希望能够做到:
var repos = GetByInterfaceAndGeneric(typeof(IRepository<>), typeof(Foo));
Run Code Online (Sandbox Code Playgroud)
并获得一个包含类型FooRepo和的 IEnumerable DifferentFooRepo。
这与此问题非常相似,但是使用该示例,我想按两者IRepository<>和 by进行搜索User。
背景:
我正在为 Unity 构建一个编辑器扩展(尽管这个问题与 Unity 并不严格相关)。用户可以从下拉列表中选择二元运算,并对输入执行该运算,如图所示:
该代码取自教程,并在此处结合使用枚举和switch语句来实现所需的行为。
下一张图演示了图形 UI 中代码和行为之间的关系:
问题
根据我之前用其他语言编程的经验,以及我希望允许用户可扩展的操作,而不需要用户在核心代码中编辑 switch 语句,我希望生成的代码看起来像这样(无效) C#代码:
... snip ...
// OperatorSelection.GetSelections() is automagically populated by inheritors of the GenericOperation class
// So it would represent a collection of types?
// so the confusion is primarily around what type this should be
public GenericOperations /* ?? */ MathOperations = GenericOperation.GetOperations();
// this gets assigned by the editor when the user clicks
// the dropdown, but I'm unclear on …Run Code Online (Sandbox Code Playgroud) c# ×8
reflection ×6
.net ×4
generics ×2
asp.net-mvc ×1
assemblies ×1
buildmanager ×1
dispatch ×1
inheritance ×1
plugins ×1
state ×1