标签: reflection

Type.GetType无法从已加载的程序集创建类型

我有程序使用Asssembly.LoadFrom方法加载程序集.一段时间后,我尝试用来Type.GetType从该程序集创建一个类型(使用AssemblyQualifiedName),但该方法返回null.如果我将它设置为抛出异常,它会告诉我

无法加载文件或程序集"..."或其依赖项之一.该系统找不到指定的文件.

但我确信已经加载完全相同的程序集(它显示在AppDomain.CurrentDomain.GetAssemblies()列表中).

任何人都知道可能出现的问题和/或如何解决这个问题?

.net c# reflection assemblies

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

反射类获取任何对象的所有属性

我需要创建一个函数来获取对象的所有属性(包括子对象)这是我的错误记录功能.现在我的代码总是返回0个属性.请让我知道我做错了什么,谢谢!

public static string GetAllProperiesOfObject(object thisObject)
{
    string result = string.Empty;
    try
    {
        // get all public static properties of MyClass type
        PropertyInfo[] propertyInfos;
        propertyInfos = thisObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Static);//By default, it will return only public properties.
        // sort properties by name
        Array.Sort(propertyInfos,
                   (propertyInfo1, propertyInfo2) => propertyInfo1.Name.CompareTo(propertyInfo2.Name));

        // write property names
        StringBuilder sb = new StringBuilder();
        sb.Append("<hr />");
        foreach (PropertyInfo propertyInfo in propertyInfos)
        {
            sb.AppendFormat("Name: {0} | Value: {1} <br>", propertyInfo.Name, "Get Value");
        }
        sb.Append("<hr />");
        result = sb.ToString();
    }
    catch …
Run Code Online (Sandbox Code Playgroud)

c# reflection

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

C#:是否有通用的方法将方法调用转发给另一个对象(具有相同的接口)?

有没有办法以通用的方式实现这种模式?

调度程序对象和一组工作程序对象都来自同一个接口.

调度到调度程序对象的任何方法都需要被调度(转发)到一个工作者对象(带有所有参数).

每个方法都需要发现它自己的名字,在worker对象中找到相应的方法,发现参数,然后进行调用.如果可能,不使用变量参数机制.

有办法做到这一点吗?反射?代码生成?

c# reflection

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

getDeclaredMethods()和隐藏的超类静态方法

根据http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29,Class.getDeclaredMethods()应该只包含"由类声明的方法" ".但是,我在下面的测试代码中得到了一些非常令人惊讶的结果:

import java.util.Arrays;

class A {
 public static A m1() { return null; }
 public static A m2() { return null; }
 public static A m3() { return null; }
}

class B extends A {
 public static A m1() { return null; }
 public static B m2() { return null; }
// public static Object m3() { return null; } won't compile
}


public class Scratch {
 public static void main(final String[] args) {
  System.out.println(Arrays.asList(B.class.getDeclaredMethods()));
 } …
Run Code Online (Sandbox Code Playgroud)

java reflection static static-methods method-hiding

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

方法工厂 - 案例与反思

前几天我遇到了一些代码,我想知道这是否是最好的方法.我们有一个方法,它根据传入的字符串从一些Web表单数据中获取一个字符串对象.当前,它使用反射来计算要采取的操作,但我想知道switch语句是否会更好.

例:

编辑:我为Lucerno指出的代表添加了第三个选项

public class ObjectManipulator
{
    private void DoX(object o) { }
    private void DoY(object o) { }
    private void DoZ(object o) { }

    public void DoAction(string action, object o)
    {
        switch (action)
        {
            case "DoX":
                DoX(o);
                break;
            case "DoY":
                DoY(o);
                break;
            case "DoZ":
                DoZ(o);
                break;
            default:
                throw new Exception(string.Format(
                    "Cannot locate action:{0}", action));
        }
    }

    public void DoActionViaReflection(string action, object o)
    {
        MethodInfo method = typeof(ObjectManipulator).
            GetMethod(action, new Type[] { typeof(object) });
        if (method == null)
        {
            throw …
Run Code Online (Sandbox Code Playgroud)

c# reflection factory switch-statement

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

XmlSerializer,"指定"后缀和IReflect

我发现如果可序列化的Field/Property有一个Boolean类型的相应字段,其字段名称为"Specified"后缀,则XmlSerializer有条件地从序列化过程中排除该Field/Property.太好了!

所以,我想避免这些字段的定义,并在运行时动态添加它们......

阅读本文,我发现了一个有趣的界面IReflect,我可以用它来"模拟"XmlSerializer实例用来排除某些字段的动态字段.

这会有用吗?

c# reflection xml-serialization

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

如何获取间接实现的通用接口的实际类型参数?

我有一个参数化的界面,以多种不同的方式实现.在运行时,我需要弄清楚,给定一个实现该接口的任意对象,接口的实际类型参数是什么.

这是一个解释问题的片段,以及解决问题的中途尝试(也在ideone.com上):

import java.util.*;
import java.lang.reflect.*;

interface Awesome<X> { }
class Base<E> implements Awesome<Set<E>> { }
class Child extends Base<List<Integer>> { }

class AwesomeExample {      
    public static void main(String[] args) {
        Awesome<Set<List<Integer>>> x = new Child();

        System.out.println(
            ((ParameterizedType)
                Child.class.getGenericSuperclass()
            ).getActualTypeArguments()[0]
        );
        // prints "java.util.List<java.lang.Integer>"

        System.out.println(
            ((ParameterizedType)
                Base.class.getGenericInterfaces()[0]
            ).getActualTypeArguments()[0]
        );
        // prints "java.util.Set<E>"        

        investigate(x);
        // we want this to print "Set<List<Integer>>"
    }

    static void investigate(Awesome<?> somethingAwesome) {
        // how to do this?
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来在运行时有足够的泛型类型信息来推断:

  • Child extends Base<List<Integer>>
  • Base<E> …

java generics reflection interface

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

在匿名方法中使用MethodInfo.GetCurrentMethod()

public static void Main(string[] args)
{
    Action a = () => Console.WriteLine(MethodInfo.GetCurrentMethod().Name);
    a();
}
Run Code Online (Sandbox Code Playgroud)

这段代码将返回一个不起眼的字符串,如下所示:<Main>b__0.

有没有办法忽略匿名方法并获得更易读的方法名称?

.net c# reflection anonymous-methods

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

如何确定ParameterInfo是否是泛型类型?

我有MethodInfo一个GenericMethodDefinition.如:CallMethod<T>(T arg, string arg2).GetParameters()方法将为我提供两个ParameterInfo对象,第一个是通用的,第二个不是.如何让ParameterInfo告诉我它是通用的?如果它有约束怎么办?

c# generics reflection methodinfo parameterinfo

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

如何从Action <T>获取方法的自定义属性?

如何从Action<T>委托中获取方法的自定义属性?

例:

//simple custom attribute
public class StatusAttribute : Attribute
{

    public string Message { get; set; } = string.Empty;
}

// an extension methodto wrap MethodInfo.GetCustomAttributes(Type, Bool) with
// generics for the custom Attribute type
public static class MethodInfoExtentions
{
    public static IEnumerable<TAttribute> GetCustomAttributes<TAttribute>(this MethodInfo methodInfo, bool inherit) where TAttribute : Attribute
    {
        object[] attributeObjects = methodInfo.GetCustomAttributes(typeof(TAttribute), inherit);
        return attributeObjects.Cast<TAttribute>();
    }
}

// test class with a test method to implment the custom attribute
public class Foo …
Run Code Online (Sandbox Code Playgroud)

c# reflection

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