标签: reflection

反射的铸造误差

我有一个使用通过接口管理的插件的应用程序然后动态加载插件类并将它们转换为接口以使用它们.

我有以下代码行,假设IPlugin是我的界面.

IPlugin _plugin = (IPlugin)Activator.CreateInstance(oInfo.Assembly, oInfo.FullyQualifiedName)
Run Code Online (Sandbox Code Playgroud)

这应该非常简单,创建实例并将其强制转换为接口.我知道程序集和完全限定的名称值是正确的,但我得到以下异常.

Exception = System.InvalidCastException:无法将类型为"System.Runtime.Remoting.ObjectHandle"的对象强制转换为"MyNamespace.Components.Integration.IPlugin".at MyNamespace.Components.Integration.PluginProxy..ctor(Int32 instanceId)

有什么想法会导致这种情况吗?

c# reflection interface

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

使用反射来获取静态类的列表

很多问题都很接近,但没有人回答我的问题......

如何在C#3.5中使用反射来获取程序集中的所有静态类.我已经定义了所有类型,但没有IsStatic属性.计数0构造函数非常慢,也无法正常工作.

任何提示或一行代码?:-)

克里斯

c# reflection static

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

从调用返回另一个代理的Java.lang.reflect.Proxy在赋值时导致ClassCastException

所以我正在玩geotools,我想我会代理他们的一个数据访问类,并跟踪它们在代码中的使用方式.

我编写了一个动态代理并在其中包含了一个FeatureSource(接口),并且很高兴.然后我想看看一些由featureSource返回,以及在传递对象,因为FeatureSource做的主要工作是返回的FeatureCollection(FeatureSource类似于一个SQL数据源,并以的FeatureCollection SQL语句).

在我的InvocationHandler我刚刚通过的调用底层对象,打印出目标类/方法/指定参数和结果,我去了,但是对于返回的FeatureCollection(另一个接口)调用,我裹着该对象在我的代理服务器(在相同的类,但一个新的实例,应该不重要吗?)并返回它.BAM!Classcast异常:

java.lang.ClassCastException: $Proxy5 cannot be cast to org.geotools.feature.FeatureCollection  
    at $Proxy4.getFeatures(Unknown Source)  
    at MyClass.myTestMethod(MyClass.java:295)  
Run Code Online (Sandbox Code Playgroud)

调用代码:

FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = ... // create the FS
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) FeatureSourceProxy.newInstance(featureSource, features);
featureSource.getBounds();// ok
featureSource.getSupportedHints();// ok

DefaultQuery query1 = new DefaultQuery(DefaultQuery.ALL);
FeatureCollection<SimpleFeatureType, SimpleFeature> results = featureSource.getFeatures(query1); //<- explosion here
Run Code Online (Sandbox Code Playgroud)

代理人:

public class FeatureSourceProxy  implements java.lang.reflect.InvocationHandler {

private Object target;
private List<SimpleFeature> features;

public static Object newInstance(Object obj, List<SimpleFeature> features) {
return java.lang.reflect.Proxy.newProxyInstance(
    obj.getClass().getClassLoader(), 
    obj.getClass().getInterfaces(), 
    new FeatureSourceProxy(obj, features)
);
}

private …
Run Code Online (Sandbox Code Playgroud)

java reflection proxy classcastexception geotools

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

您如何反映应用于返回值的属性?

考虑以下:

[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class NotNullAttribute : Attribute
{
}

public class Class1
{
    [return: NotNull]
    public static string TestMethod([NotNull] string arg)
    {
        return arg + " + " + arg;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用System.Reflection,您是否会看到NotNullAttribute属性已应用于方法的返回值?如果你不能,[return:]语法背后的目的是什么?

c# reflection attributes postsharp

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

如何确定.NET Type是否是自定义结构?

如何编写一个简单的方法,检查具体类型是否是自定义结构(使用public struct { };)创建.

检查Type.IsValueType是不够的,因为这也是千真万确的int,long等,并增加了检查,!IsPrimitiveType将不排除decimal,DateTime也许有些其他值类型.我知道大多数内置值类型实际上是"结构",但我只想检查"自定义结构"

这些问题大致相同但没有我需要的答案:

编辑:从提到的答案"检查'系统'前缀"是最稳定的(虽然它仍然是一个黑客).我最终决定创建一个你必须用它来装饰结构的属性,以便框架将它作为自定义结构来选择.(我想的另一个选择是创建一个空接口,让struct实现那个空接口,但属性方式似乎更优雅)

这是我原来的自定义结构检查器,如果有人感兴趣:

type.IsValueType && !type.IsPrimitive && !type.Namespace.StartsWith("System") && !type.IsEnum
Run Code Online (Sandbox Code Playgroud)

.net c# reflection struct types

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

Java:如何加载已经在类路径上的类(及其内部类)?

如何加载已经在类路径上的类,实例化它,并实例化其中定义的任何内部类?

例如:

public class TestClass {


    public class InnerClass { }

}
Run Code Online (Sandbox Code Playgroud)

java reflection classloader

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

如何反射打印方法体?

现在我有

private static void getMethods(Class<? extends Object> clazz) {
    Method[] declaredMethods = clazz.getDeclaredMethods();
    for (Method aMethod : declaredMethods) {
      aMethod.setAccessible(true);

      // Print the declaration
      System.out.print(Modifier.toString(aMethod.getModifiers()) + " "
          + aMethod.getReturnType().getSimpleName() + " " + aMethod.getName());

      // Get Parameter Types
      getParameters(aMethod);

      //Empty Body
      System.out.println("{}\n");
    }
  }
Run Code Online (Sandbox Code Playgroud)

它反射性地打印大多数信息,但创建一个空体.如何添加Java的反射特性来打印方法体?

java reflection

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

获取参数化类参数的类型?

我有以下课程

public class MyClass<T> {
    public Class<T> getDomainClass() {
          GET THE CLASS OF T
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经google了这个问题,我找到的所有答案都告诉我使用getGenericSuperClass(),但这个方法的问题是我必须有第二个类扩展MyClass而我不想这样做.我需要的是获得具体类的参数化类型?

java generics reflection

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

如何从存储在Variable中的字符串调用Java函数

可能重复:
在Java和C中在运行时调用名为"string"的方法

我需要能够调用一个函数,但函数名存储在一个变量中,这是可能的.例如:

public void foo ()
{
     //code here
}

public void bar ()
{
     //code here
}

String functionName = "foo";
Run Code Online (Sandbox Code Playgroud)

//我需要根据functionName调用函数

任何帮助都会很棒,谢谢

java reflection function-call

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

如何将Java中的类型与反射进行比较

我有以下代码,现在我有类型,但我需要进行某种切换,以了解类型是否为String的例子,然后处理字符串.那么如何检查Type t是否为String类型?

Type t = bean.getClass().getDeclaredField(fieldName).getType();
Run Code Online (Sandbox Code Playgroud)

java reflection

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