我正在开发一个插件系统,它可以加载包含在指定文件夹中的.dll.然后我使用反射来加载程序集,遍历它们包含的类型并识别实现我的IPlugin接口的任何类型.
我正在使用类似于以下内容的代码进行检查:
foreach(Type t in myTypes )
{
if( typeof(IPlugin).IsAssignableFrom(t) )
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,IsAssignableFrom()在返回true时保持返回false.我已经尝试t通过明确地给它一个应该传递的类型替换它,并且它工作正常,但由于某种原因它不能使用从加载的程序集返回的类型.为了使事情变得陌生,代码在我的同事的机器上工作正常,但在我的机器上却没有.
有谁知道任何可能导致这种行为的事情?
谢谢
我正在编写小而非常DRY的框架,它严重依赖于元数据.我想知道是否有办法获取方法参数名称,即给定一些方法
public void a(int myIntParam, String theString) { ... }
Run Code Online (Sandbox Code Playgroud)
得到字符串"myIntParam"和"theString".
我知道我可以注释参数,但这不会很好......
public void a(
@Param("myIntParam") int myIntParam,
@Param("theString") String theString
) { ... }
Run Code Online (Sandbox Code Playgroud) 可以说我有这门课
class Child {
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Container {
public List<Child> { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在研究各种各样的反序列化器,我希望能够Child从检索到的数据中创建和填充列表.我已经做到这一点了(我已经为这个例子减少了很多其他类型的处理,所以它不必要地"iffy"但是请耐心等待):
var props = typeof(Container).GetProperties();
foreach (var prop in props) {
var type = prop.PropertyType;
var name = prop.Name;
if (type.IsGenericType) {
var t = type.GetGenericArguments()[0];
if (type == typeof(List<>)) {
var list = Activator.CreateInstance(type);
var elements = GetElements();
foreach (var element in elements) {
var item = Activator.CreateInstance(t); …Run Code Online (Sandbox Code Playgroud) 我想用
Class.getMethod(String name, Class... parameterTypes)
Run Code Online (Sandbox Code Playgroud)
找到我需要使用给定参数调用的方法,但显然如Bug 6176992中所述,Java不包括那里的自动装箱.因此,如果我的反射类有一个带有(String,int)签名的方法,你仍然会得到一个带有{String.class,Integer.class}数组作为参数的NoSuchMethodException.
这有什么解决方法吗?我能想到的唯一方法是调用getMethod()来处理原始和非原始类型的每个排列,我并不是真的想做.
编辑:为了使它更清楚:我很清楚原始类型类,但我不知道它们如何帮助解决我的问题.我的parameterTypes数组来自某个地方,我知道它只会返回非原始类型.我不能假设接口只会用原始类型声明,这正是我的问题:
public interface TestInterface()
{
public void doTest(Integer i1, int i2, double d3, Double d);
}
Class<?>[] classes = { Integer.class, Integer.class, Double.class, Double.class }
// Due to autoboxing I should become the doTest method here, but it doesn't work
TestInterface.class.getMethod("doTest", classes);
Run Code Online (Sandbox Code Playgroud) 我有一个接口IExample和一组类ClassOne,ClassTwo并且ClassThree都在不同的命名空间中定义.我可能会删除其中一个类,或者在开发的后期阶段在新的位置添加一个新类.
现在,我想找到IExample在运行时实现的所有类型,并实例化它们.(我事先知道没有类实现IExample将需要任何构造函数参数,但我不知道如何在代码中指定它,所以它是我 - 而不是编译器 - 知道......)
这可能吗?我该怎么做呢?
更新:我现在尝试了几种建议的方法,但是就所有这些方法而言Activator.CreateInstance(type),我得到了一个System.MissingMethodException,因为我"无法创建接口的实例".这是我的代码:
var tasks = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => typeof(IBootstrapperTask).IsAssignableFrom(t))
// This line is where it fails
.Select(t => Activator.CreateInstance(t) as IBootstrapperTask)
.ToArray();
new AutoMapperBootstrapper(tasks).Initialize();
Run Code Online (Sandbox Code Playgroud)
没有这个as条款我没有看到任何异常,但我得到了一个object[],我需要一个IBootstrapperTask[]在摘录的最后一行的构造函数.我已经尝试了各种方法来投射它,但似乎都没有效果.
我想调用的方法(我知道它是公开的,但我需要使用反射):
public byte[] myMethod()
Run Code Online (Sandbox Code Playgroud)
我得到这样的Method对象并m包含myMethod()(我用调试器检查)
Method m = Class.forName(MyClass.class.getName()).getDeclaredMethod("myMethod");
Run Code Online (Sandbox Code Playgroud)
最后,我需要调用m并将结果传递给对象:
byte[] myBytes = null;
m.invoke(myBytes);
Run Code Online (Sandbox Code Playgroud)
没有抛出异常但myBytes保持为null ...我也尝试了以下方法但没有取得更多成功:
m.invoke(myBytes, (Object[])null);
Run Code Online (Sandbox Code Playgroud)
如何获得调用myBytes的结果?
有没有编写递归方法的简单方法,它会为类中的泛型类型提供"用户友好"的名称Type?
例如,对于以下代码,我想要"List <Dictionary <Int >>",而不是以下代码给出的简写或全名:
var list = new List<Dictionary<int, string>>();
var type = list.GetType();
Console.WriteLine(type.Name);
Console.WriteLine(type.FullName);
Run Code Online (Sandbox Code Playgroud) 为什么PropertyInfo获取和设置属性的方法如此之慢?如果我使用构建委托Reflection.Emit,它会快得多.
他们是否正在做一些重要的事情,以便他们采取的时间是合理的?这是...我失去的东西通过Reflection.Emit建立代表,而不是usign的GetValue和SetValue的的PropertyInfo(预留的发展速度)?
PS:请证明,不仅要猜测!
我有一个泛型类,其中我有一个函数来获取传递的泛型对象的属性.它如下所示.
public class ExportToCsv<T>
where T: class
{
public ExportToCsv(List<T> obj)
{
this.Data = obj;
}
public StringBuilder CreateRows()
{
IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我通过从下面的对象(类)中选择来传递对象,它工作正常并返回属性
//GetLeadingRoutingRecords returns a class/object
var result = from obj in GetLeadRoutingRecords()
select new
{
LeadRoutingId = obj.LeadRoutingID,
Make = obj.Make
};
Run Code Online (Sandbox Code Playgroud)
并将结果传递给 result.ToList();
但是当我尝试通过为下面的属性创建一个类来创建我自己的匿名对象时,它无法返回任何属性
注意:下面的代码是在循环中调用的,它运行良好并传递给上面的函数可以通过调试查看所有值.
public CsvReport function return(){
return new CsvReport
{
ShopName = this.val,
TargetVehicleName = val
}.ToList();
}
Run Code Online (Sandbox Code Playgroud)
我为上面的匿名对象编写的类如下所示:
public class CsvReport
{
public string ShopName { get; set; } …Run Code Online (Sandbox Code Playgroud) 如何列出在Swift中实现给定协议的所有类?
假设我们有一个例子:
protocol Animal {
func speak()
}
class Cat:Animal {
func speak() {
print("meow")
}
}
class Dog: Animal {
func speak() {
print("Av Av!")
}
}
class Horse: Animal {
func speak() {
print("Hurrrr")
}
}
Run Code Online (Sandbox Code Playgroud)
这是我目前的(不可编译)方法:
func getClassesImplementingProtocol(p: Protocol) -> [AnyClass] {
let classes = objc_getClassList()
var ret = [AnyClass]()
for cls in classes {
if class_conformsToProtocol(cls, p) {
ret.append(cls)
}
}
return ret
}
func objc_getClassList() -> [AnyClass] {
let expectedClassCount = objc_getClassList(nil, 0)
let allClasses …Run Code Online (Sandbox Code Playgroud)