我在PHP中循环遍历一个类名数组,通过get_declared_classes()获取.
如何检查每个类名以检测该特定类是否为抽象类?
我需要在运行时加载的程序集中执行一个方法.现在我想在方法调用后卸载那些已加载的程序集.我知道我需要一个新的AppDomain,所以我可以卸载库.但在这里,出现了问题.
要加载的程序集是我的插件框架中的插件.他们根本没有切入点.我所知道的是它们包含一些实现给定接口的类型.旧的非AppDomain代码看起来像这样(稍微缩短):
try
{
string path = Path.GetFullPath("C:\library.dll");
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Assembly asm = Assembly.LoadFrom(path);
Type[] types = asm.GetExportedTypes();
foreach (Type t in types)
{
if ((t.GetInterface("IStarter") != null) && !t.IsAbstract)
{
object tempObj = Activator.CreateInstance(t);
MethodInfo info = t.GetMethod("GetParameters");
if (info != null)
{
return info.Invoke(tempObj, null) as string;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(String.Format("Damn '{0}'.", ex.Message), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("MyProject.View,"))
{
string path = Path.GetFullPath("C:\view.dll"));
return …Run Code Online (Sandbox Code Playgroud) ParameterizedType parameterized =
(ParameterizedType) List.class.getMethod("iterator").getGenericReturnType();
Type raw = parameterized.getRawType();
Run Code Online (Sandbox Code Playgroud)
ParameterizedType#getRawType()返回a Type,而不是a Class<?>(虽然我java.lang.Class现在已经实现Type).有没有一个很好的理由为什么getRawType()不声明它的返回类型Class<?>?是否存在getRawType()可能不是结果的极端情况Class<?>?
这已经足够让人感到挣扎j.l.r.Type; 这似乎是一个他们本可以为我们拯救一个沮丧的实例.
当我进入irb并输入一个不存在的命令时,我收到一个错误说明
"undefined local variable or method 'my_method' for main:Object (NameError)"
Run Code Online (Sandbox Code Playgroud)
有没有办法获得可用的局部变量或方法的列表?这对于探索ruby非常有用.
使用类型标签,我能够看到某些类型的参数:
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> typeOf[List[Int]]
res0: reflect.runtime.universe.Type = List[Int]
Run Code Online (Sandbox Code Playgroud)
但我只是不知道如何以一般方式以编程方式从那里获得"Int".
(我已经在REPL中闲逛了一个小时,在Type上尝试排列,看看我能从中得到什么......我得到了很多东西,表明这是一个"列表",但是找到了好运"Int"!我真的不想解析toString()输出......)
丹尼尔·索布拉尔(Daniel Sobral)在这里有一个很好的(通常)快速概述,其中他非常接近我正在寻找的东西,但(显然)只有你碰巧知道,对于那个特定的类,一些特定的方法,其类型可以是询问:
scala> res0.member(newTermName("head"))
res1: reflect.runtime.universe.Symbol = method head
scala> res1.typeSignatureIn(res0)
res2: reflect.runtime.universe.Type = => Int
Run Code Online (Sandbox Code Playgroud)
但我希望有一些更通用的东西,它不涉及在声明的方法列表中生根,并希望其中一个将捕获(并因此泄露)标签的当前类型信息.
如果Scala可以如此轻松地打印 "List [Int]",那么为什么它很难发现"Int"的一部分 - 没有求助于字符串模式匹配?或者我只是遗漏了一些非常非常明显的东西?
scala> res0.typeSymbol.asInstanceOf[ClassSymbol].typeParams
res12: List[reflect.runtime.universe.Symbol] = List(type A)
scala> res12.head.typeSignatureIn(res0)
res13: reflect.runtime.universe.Type =
Run Code Online (Sandbox Code Playgroud)
格儿...
有没有一个好地方可以获得Scala 2.10反射API的全面教程?我看到过去两年里写的很多博客文章,但这些帖子的许多细节已经过时了.
现在2.10是公开的,是否有任何文件可以指导我如何反思?
考虑以下类层次结构:基类A,继承自A的类B和C以及继承自B的类D.
public class A {...}
public class B : A {...}
public class C : A {...}
public class D : B {...}
Run Code Online (Sandbox Code Playgroud)
我可以使用以下代码来查找A的所有子类,包括D:
var baseType = typeof(A);
var assembly = typeof(A).Assembly;
var types = assembly.GetTypes().Where(t => t.IsSubclassOf(baseType));
Run Code Online (Sandbox Code Playgroud)
但是我只需要找到A的直接子类(例子中的B和C)并排除所有不直接从A继承的类(例如D).知道怎么做吗?
我一直在使用Reflections,并希望获得为属性声明的所有属性.PropertInfo类下有两个属性是CustomAttributes和Attributes.
根据MSDN,他们解释如下:
属性:
此属性表示与成员关联的属性.所有成员都有一组与特定成员类型相关的属性.属性属性允许用户知道此属性是否为默认属性,SpecialName属性等.
注意:PropertyInfo.Attributes页面中给出的代码示例甚至不起作用.
自定义属性:
包含应用于此成员的所有自定义属性的数组,如果未定义任何属性,则包含零元素的数组.
然而,当我为他们运行此代码,Attributes没有返回而CustomAttributes回报Required.
void Main()
{
var attributes = typeof(Myproperty).GetProperty("Caption").CustomAttributes;
//var attributes = typeof(Myproperty).GetProperty("Caption").Attributes;
attributes.Dump(); //Dump is a LinqPad method which dumps everything to the outpu window
}
public class Myproperty
{
private string caption = "Default caption";
[Required]
public string Caption
{
get{return caption;}
set {if(caption!=value) {caption = value;}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在查看与Razor View引擎相关的调试器中的一些代码,我注意到一些类型出现在Debugger中,在类型名称的末尾有一个尾随点字符,例如:
{Nancy.ViewEngines.Razor.RazorViewEngine.}
有谁知道这表明了什么?在对象上指定强制转换时使用它是无效的语法,因此我对它在调试器中的含义感到好奇.
编辑:根据@Damien_The_Unbeliever的要求,调试器中变量的屏幕截图:

我正在看的代码:
public TCompiledView GetOrAdd<TCompiledView>(
ViewLocationResult viewLocationResult, Func<ViewLocationResult, TCompiledView> valueFactory)
{
TCompiledView compiledView = default(TCompiledView);
compiledView = (TCompiledView)this.cache.GetOrAdd(viewLocationResult, x => valueFactory(x));
Run Code Online (Sandbox Code Playgroud)
为了给出更多背景知识,我们尝试将日志记录添加到我们的Nancy View Cache中,以调查Razor Views抛出编译错误的间歇性问题,但这与问题无关.
我有一些像这样的代码:
class ReflectiveJsonFormat[T:TypeTag] extends JsonFormat[T] {
def write(x: T) : JsValue = {
val t = typeOf[T]
val getters = t.declarations.filter { s => s.isMethod && s.asMethod.isGetter }
val mirror = runtimeMirror(this.getClass.getClassLoader)
val instanceMiror = mirror.reflect(x)
}
}
Run Code Online (Sandbox Code Playgroud)
最后一行失败了:
没有ClassTag可用于T
我以为TypeTag是比一个更多的信息ClassTag?我能得到ClassTag从TypeTag?如果没有,是否有一些语法可以说T有两个上下文边界 - 两个TypeTag和ClassTag?或者,您将如何修复此代码?