标签: reflection

如果在运行时只知道类型参数,如何调用泛型方法?

我有这个方法:

public List<T> SomeMethod<T>( params ) where T : new()
Run Code Online (Sandbox Code Playgroud)

所以SomeMethod如果我知道类型,我想称之为好的:

SomeMethod<Class1>();
Run Code Online (Sandbox Code Playgroud)

但如果我只是Class1在运行时我无法调用它?

那么如何SomeMethod使用未知的T型调用?我用反射得到了Type.

我有类型的类型,但SomeMethod<Type | GetType()>不起作用.

更新7. May:

以下是我想要实现的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleApplication63
{
    public class DummyClass
    {
    }

    public class Class1
    {
        public string Name;
    }

    class AssemblyTypesReflection
    {
        static void Main(string[] args)
        {
            object obj = new Class1() { Name = "John" } ;

            Assembly assembly = Assembly.GetExecutingAssembly();
            var AsmClass1 = …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection

7
推荐指数
2
解决办法
5041
查看次数

检测scala对象中的内部对象

我想获得一个scala对象的内部对象列表.示例代码:

object Outer {
  val v = "-"
  def d = "-"
  object O1
  object O2
}

object Main {
  def main(args: Array[String]) {
    Outer.getClass.getDeclaredMethods.map(_.getName) foreach println  // prints d and v
    // Outer.getClass.get ... Objects???
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以找到v和d,但我怎样才能找到O1和O2?

reflection scala object

7
推荐指数
1
解决办法
1470
查看次数

如何在Java中找出声明的标识符类型?

我有一个简单的类Apple扩展自另一个简单的类Fruit.

在运行时,我可以使用

Fruit fruit = new Apple();

fruit.getClass();
Run Code Online (Sandbox Code Playgroud)

获取实际类型的水果对象,即Apple.class.

我也可以使用fruit instanceof Apple,并fruit instanceof Fruit验证这个水果对象是Apple还是Fruit的实例.这两个表达式都返回true,这是正常的.

但有没有办法确定fruit标识符的声明类型?在这种情况下是Fruit.

java reflection polymorphism runtime class

7
推荐指数
1
解决办法
2793
查看次数

是否可以使用SBT的反射?

我试图用SBT生成一些样板(对我来说这是一个全新的工具).我使用无形 sbt文件作为我的主要参考任务.我已经看到这个项目从头开始使用代码生成,但我的情况略有不同,因为我想从另一个类生成一些类.我假装使用新的Scala 2.10.0-M4反射功能.从SBT构建中获得反射需要什么基本配置?

到目前为止,sbt无法找到scala.reflect.runtime.universe包,我不知道问题是来自新的Scala jar分区还是来自错误的配置.此外,我的SBT 约说:

[info] This is sbt 0.13.0-20120530-052139
[info] The current project is {file:/home/jlg/sandbox/abc/}abc
[info] The current project is built against Scala 2.10.0-SNAPSHOT
[info] 
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.2
Run Code Online (Sandbox Code Playgroud)

那么,是否有人知道使用SBT生成源代码的其他项目?

reflection scala sbt scala-2.10

7
推荐指数
1
解决办法
631
查看次数

Spring vs Java反射

我需要动态创建对象.我使用Spring创建了一个类名映射.现在我可以用了

弹簧 ApplicationContext.getbean(className)

要么

Java Class.forName(className).newInstance().

哪种方法更有效?

谢谢.

java reflection spring

7
推荐指数
1
解决办法
1万
查看次数

无法使用getDeclaredFields()来检索Scala类的字段

我正在尝试使用Scala的Java库(JOhm)并注意到当lib试图用类似的东西读取我的Scala类的字段时它会失败model.getClass().getDeclaredFields().

然后我决定尝试使用Scala解释器中的简单示例:

scala> import java.lang.reflect.Field;
import java.lang.reflect.Field

scala> class myClass(attribute1: String, attribute2: String, attribute3: String)
defined class myClass

scala> val myInstance = new myClass("value1", "value2", "value3")
myInstance: myClass = myClass@7055c39a

scala> myInstance.getClass().getDeclaredFields()
res0: Array[java.lang.reflect.Field] = Array()
Run Code Online (Sandbox Code Playgroud)

实际上,我们根本没有任何领域.

现在,如果我尝试这个怎么办:

scala> class myClass2(attribute1: String, attribute2: String, attribute3: String) { override def toString = this.attribute1 }
defined class myClass2

scala> val myInstance2 = new myClass2("value1", "value2", "value3")
myInstance2: myClass2 = value1

scala> myInstance2.getClass().getDeclaredFields()
res1: Array[java.lang.reflect.Field] = Array(private final java.lang.String myClass2.attribute1)
Run Code Online (Sandbox Code Playgroud)

因此,如果使用其中一个类'方法中的一个字段,则可以通过getDeclaredFields()找到它.我在这里错过了什么?

java reflection scala

7
推荐指数
1
解决办法
3178
查看次数

从另一个可执行文件调用内部类中的函数

我想从我自己的代码中调用.net可执行文件中的函数.我用反射器看到了这个:

namespace TheExe.Core
{
    internal static class AssemblyInfo
    internal static class StringExtensionMethods
}
Run Code Online (Sandbox Code Playgroud)

在命名空间中,TheExe.Core是我感兴趣的函数:

internal static class StringExtensionMethods
{
    // Methods
    public static string Hash(this string original, string password);
    // More methods...
}
Run Code Online (Sandbox Code Playgroud)

使用此代码我可以看到哈希方法,但我该如何调用它?

Assembly ass = Assembly.LoadFile("TheExe");
Type asmType = ass.GetType("TheExe.Core.StringExtensionMethods");
MethodInfo mi = asmType.GetMethod("Hash", BindingFlags.Public | BindingFlags.Static);
string[] parameters = { "blabla", "MyPassword" };

// This line gives System.Reflection.TargetParameterCountException
// and how to cast the result to string ?
mi.Invoke(null, new Object[] {parameters});
Run Code Online (Sandbox Code Playgroud)

.net c# reflection

7
推荐指数
1
解决办法
9160
查看次数

带有参数化类型的Java getConstructor(类型)

如果我有一个带有构造函数的类,该构造函数采用参数化的泛型类型:

public class Foo {
    public Foo(Map<String, Object> data) {
      ...
    }
}
Run Code Online (Sandbox Code Playgroud)

...如果我想调用,如何引用该参数化Map?

Constructor constructor = cls.getConstructor(/*the Map class! */)
Run Code Online (Sandbox Code Playgroud)

(clsFoo类在哪里.)

我想做的事情如下:

Constructor constructor = cls.getConstructor(Map<String,Object>.class);
Run Code Online (Sandbox Code Playgroud)

......但这不起作用.

我相信这有一个简单的答案!

java generics reflection

7
推荐指数
1
解决办法
3101
查看次数

Reflection获取对象属性以对列表进行排序

我想通过存储在其中的对象的属性对c#中的列表进行排序.我有这个:

if (sortColumn == "Login")
{
    if (sortDir == "ASC")
    {
        filteredList.Sort((x, y) => string.Compare(x.Login, y.Login, true));
    }
    else
    {
        filteredList.Sort((x, y) => string.Compare(y.Login, x.Login, true));
    }
 }
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我想更通用,以便不必知道要排序的字段.我想过这样的事情:

//With sortColumn = "Login";
if (sortDir == "ASC")
{
    filteredList.Sort((x, y) => string.Compare(x.GetType().GetProperty(sortColumn), y.GetType().GetProperty(sortColumn), true));
}
else
{
    filteredList.Sort((x, y) => string.Compare(y.GetType().GetProperty(sortColumn), x.GetType().GetProperty(sortColumn), true));
}
Run Code Online (Sandbox Code Playgroud)

显然这不起作用,但这就是我想要的.有可能吗?

谢谢.

c# sorting reflection

7
推荐指数
1
解决办法
2975
查看次数

Type.GetType不适用于泛型类?

Type t = Type.GetType("BLL.MyLayers.TestLayer,BLL");
Run Code Online (Sandbox Code Playgroud)

对于泛型类,t始终为null.

当我尝试获取普通类的类型时,t不为空.

为什么这样或我做错了什么?

c# reflection types

7
推荐指数
1
解决办法
2391
查看次数

标签 统计

reflection ×10

c# ×4

java ×4

scala ×3

generics ×2

.net ×1

class ×1

object ×1

polymorphism ×1

runtime ×1

sbt ×1

scala-2.10 ×1

sorting ×1

spring ×1

types ×1