我和朋友正在测试使用编译表达式来创建对象,而不是Activator.CreateInstance<T>遇到一些有趣的结果.我们发现当我们在每台机器上运行相同的代码时,我们看到完全相反的结果.他得到了预期的结果,在编译表达式中表现明显更好,而我惊讶地看到Activator.CreateInstance<T>表现为2x.
两台计算机都运行在.NET 4.0中编译
计算机1安装了.NET 4.5.电脑2没有.
计算机1超过100000个对象:
45ms - Type<Test>.New()
19ms - System.Activator.CreateInstance<Test>();
Run Code Online (Sandbox Code Playgroud)
计算机2超过100000个对象:
13ms - Type<Test>.New()
86ms - System.Activator.CreateInstance<Test>();
Run Code Online (Sandbox Code Playgroud)
以下是代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
namespace NewNew
{
class Program
{
static void Main(string[] args)
{
Stopwatch benchmark = Stopwatch.StartNew();
for (int i = 0; i < 100000; i++)
{
var result = Type<Test>.New();
}
benchmark.Stop();
Console.WriteLine(benchmark.ElapsedMilliseconds + " Type<Test>.New()");
benchmark = Stopwatch.StartNew();
for (int i = 0; i < 100000; …Run Code Online (Sandbox Code Playgroud) 在阅读了文章" 如何检查方法是否具有属性 "之后,我是解决让我保持清醒的问题的一步.
(我正在使用ASP.Net MVC 4)
这些接口:
public interface IFlyable
{
ActionResult Fly();
}
public interface IRunnable
{
ActionResult Run();
}
Run Code Online (Sandbox Code Playgroud)
这个抽象类:
public abstract class SuperHero : Controller
{
public void SavePeople()
{
}
}
Run Code Online (Sandbox Code Playgroud)
这个控制器:
public class SuperManController : SuperHero,IFlyable,IRunnable {
[Authorize]
public ActionResult Fly(){
// Flying...
}
[Authorize]
public ActionResult Run(){
// Running...
}
}
Run Code Online (Sandbox Code Playgroud)
这个抽象类(用于测试)
[TestClass]
public abstract class SuperHeroTest<TSuperHero>{
protected abstract TSuperHero GetSuperHero();
[TestMethod]
public void IfSuperHeroCanFlyMustHaveAuthorizeAttribute(){
var superHero=GetSuperHero();
if(superHero is IFlyable)
{ …Run Code Online (Sandbox Code Playgroud) 有Assembly.GetExecutingAssembly()和Assembly.GetCallingAssembly().请注意,GetCallingAssembly()具有一个Remark提的是,根据JIT内联的行为方式可以是可能的是,一个方法是(或不是)内联到另一个,因此GetCallingAssembly()返回不同的结果.
现在有何GetExecutingAssembly()不同?JIT内联在技术上可以内联调用的代码,GetExecutingAssembly()因此代码现在属于不同的程序集,并且取决于发生的是否也GetExecutingAssembly()可以产生不同的结果.
为什么GetExecutingAssembly()描述中没有提到JIT inining类似于GetCallingAssembly()描述的说法?
有没有办法从一个领域获得实例?
这是一个示例代码:
public class Apple {
// ... a bunch of stuffs..
}
public class Person {
@MyAnnotation(value=123)
private Apple apple;
}
public class AppleList {
public add(Apple apple) {
//...
}
}
public class Main {
public static void main(String args[]) {
Person person = new Person();
Field field = person.getClass().getDeclaredField("apple");
// Do some random stuffs with the annotation ...
AppleList appleList = new AppleList();
// Now I want to add the "apple" instance into appleList, which I think …Run Code Online (Sandbox Code Playgroud) Jersey是否提供任何方式来列出它所暴露的所有资源?也就是说,给定资源类:
package com.zoo.resource
@Path("/animals")
public class AnimalResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("dog")
public Dog getDog(){
...
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("cat")
public Cat getCat(){
...
}
}
Run Code Online (Sandbox Code Playgroud)
泽西岛是否为我提供了获取信息的方式:
GET在路径/animals/dog返回类型Dog GET在路径/animals/cat返回类型Cat(此外,它是否为我提供了一种了解AnimalResource是资源的方法?)
我希望在单元测试中可以获得这些信息,以便我可以检查我公开的每个资源是否符合外部系统的预期.我知道有一个自动化暴露了application.wadl,但是我没有看到向我显示返回类型,我不知道如何从我的测试中访问它.
所以,我最近做了一些实验,发现似乎Reflection.Emit不支持ECMA规范中的所有操作码.缺少3个操作码:
ldelem.anystelem.any no. (字首)这些操作码是否在Reflection API中不受支持,或者是否有某种方法可以生成它们?
我看到java.lang.NoClassDefFoundError: org/omg/CORBA/InterfaceDef,当我调用该方法getMethod的java.lang.Class.我想获得的getEmsSession方法EmsSeeionFactory_I使用反射TMF具体的罐子.但是当我检查了类中的类时jacorb.jar, org/omg/CORBA/InterfaceDef.class可以在类路径上找到它.我在其他论坛上看到过,我也没有在这些论坛中找到相同问题的答案.看起来这个异常是从本机VM代码抛出的.我现在不知道了.有任何想法吗 ?完整的堆栈跟踪如下
java.lang.NoClassDefFoundError: org/omg/CORBA/InterfaceDef
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getMethod0(Class.java:2670)
at java.lang.Class.getMethod0(Class.java:2688)
at java.lang.Class.getMethod(Class.java:1603)
Run Code Online (Sandbox Code Playgroud) 有没有一种方便的方法可以在Scala 2.10中将MethodSymbol方法定义树(即a DefDef)的左侧转换为?
例如,假设我想创建一个宏来获取特征的实例,并用一些调试功能包装所有特征的方法.我可以写下面的内容:
import scala.language.experimental.macros
import scala.reflect.macros.Context
object WrapperExample {
def wrap[A](a: A): A = macro wrap_impl[A]
def wrap_impl[A: c.WeakTypeTag](c: Context)(a: c.Expr[A]) = {
import c.universe._
val wrapped = weakTypeOf[A]
val f = Select(reify(Predef).tree, "println")
val methods = wrapped.declarations.collect {
case m: MethodSymbol if !m.isConstructor => DefDef(
Modifiers(Flag.OVERRIDE),
m.name,
Nil, Nil,
TypeTree(),
Block(
Apply(f, c.literal("Calling: " + m.name.decoded).tree :: Nil),
Select(a.tree, m.name)
)
)
}.toList
//...
}
Run Code Online (Sandbox Code Playgroud)
我已经省去了无聊的业务,将这些方法粘贴在一个新的匿名类中,该类实现了特性,然后实例化了该类 - 如果你感兴趣的话,你可以在这里找到一个完整的工作示例.
现在我可以写这个,例如:
scala> trait X …Run Code Online (Sandbox Code Playgroud) 我想你不能但是有没有办法将方法作为属性的参数引用?就像下面的东西?我可以回退使用字符串,但更喜欢使用编译时间来验证类型是否正确.
[LinkToAction(Something)]
public void SomethingElse()
{
}
public static void Something()
{
}
public class LinkToActionAttribute : Attribute
{
public LinkToActionAttribute(MethodInfo info)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个项目,一个scala项目和一个java项目.我的scala项目引用了构建路径中的java项目.在我的java项目中,我正在声明以下注释:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public String Name();
}
Run Code Online (Sandbox Code Playgroud)
在我的scala项目中,我正在注释一些方法.即
class MyClass {
...
@MyAnnotation(Name="Blah")
def myMethod() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
在某个地方的另一个文件中,我正在尝试提取注释.
var methods = myClassInstance.getClass().getDeclaredMethods()
var myMethod : Method = null
for (method <- methods) {
if (method.getName().equals("myMethod")) {
myMethod = method
}
}
var annotations = myMethod.getDeclaredAnnotations()
Run Code Online (Sandbox Code Playgroud)
不幸的是,annotations总是一个空数组.我做了一些根本错误的事情,还是我只是遗漏了一些小事?谢谢!
编辑
最初,我用myAnnotation两次注释myMethod,这是不正确的,正如有人指出的那样.事实证明这不是问题.我仍然得到一个空数组annotations.没有例外被抛出.
reflection ×10
c# ×4
java ×4
.net ×2
scala ×2
activator ×1
asp.net-mvc ×1
attributes ×1
corba ×1
field ×1
il ×1
instance ×1
jax-rs ×1
jersey ×1
jit ×1
lambda ×1
macros ×1
rest ×1
scala-2.10 ×1
scala-macros ×1
unit-testing ×1