标签: reflection

如何使Spring接受流利(非虚空)的设置者?

我有一个API,我正在变成一个内部DSL.因此,我的PoJos中的大多数方法返回对此的引用,以便我可以声明性地将方法链接在一起(语法糖).

myComponent
    .setID("MyId")
    .setProperty("One")
    .setProperty2("Two")
    .setAssociation(anotherComponent)
    .execute();
Run Code Online (Sandbox Code Playgroud)

我的API不依赖于Spring,但我希望通过PoJo友好,零参数构造函数,getter和setter使其成为"Spring-Friendly".问题是,当我有一个非void返回类型时,Spring似乎没有检测到我的setter方法.

在将命令链接在一起时,返回类型非常方便,所以我不想破坏我的编程API,只是为了兼容Spring注入.

Spring中有一个设置允许我使用非void setter吗?

克里斯

java reflection spring fluent-interface introspection

14
推荐指数
3
解决办法
5720
查看次数

使用Reflection调用包含通用参数的静态方法

执行以下代码时,我收到此错误"无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作."

class Program
{
    static void Main(string[] args)
    {
        MethodInfo MI = typeof(MyClass).GetMethod("TestProc");
        MI.MakeGenericMethod(new [] {typeof(string)});
        MI.Invoke(null, new [] {"Hello"});
    }
}

class MyClass
{
    public static void TestProc<T>(T prefix) 
    {
        Console.WriteLine("Hello");
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码只是我面临的实际问题的缩放版本.请帮忙.

c# generics reflection

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

不区分大小写的GetMethod?

foreach(var filter in filters)
{
    var filterType = typeof(Filters);
    var method = filterType.GetMethod(filter);
    if (method != null) value = (string)method.Invoke(null, new[] { value });
}
Run Code Online (Sandbox Code Playgroud)

是否有一种不区分大小写的方法来获取方法?

c# reflection

14
推荐指数
2
解决办法
5308
查看次数

使用反射将项添加到List <T>

我试图通过反射向IList添加项目,但在调用"添加"方法时,抛出错误"对象引用未设置".在调试时我发现GetMethod("Add")返回了一个NULL引用.

Type objTyp = typeof(MyObject); //HardCoded TypeName for demo purpose
var IListRef = typeof (List<>);
Type[] IListParam = {objTyp};          
object Result = IListRef.MakeGenericType(IListParam);

MyObject objTemp = new MyObject(); 
Result.GetType().GetMethod("Add").Invoke(Result, new[] {objTemp });
Run Code Online (Sandbox Code Playgroud)

请帮忙.

c# reflection

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

非静态方法需要目标C#

我有一个win表单应用程序,其中列表框显示方法(按属性).我试图动态调用线程中的方法,使用反射从列表框的选定值获取方法信息.但是,当调用Methodinfo.Invoke时,我得到了这个内部异常"非静态方法需要一个目标C#".

这是我的代码(请记住,我仍然是c#和编程的新手.)

private void PopulateComboBox()
{//Populates list box by getting methods with declared attributes
    MethodInfo[] methods = typeof(MainForm).GetMethods();

    MyToken token = null;
    List<KeyValuePair<String, MethodInfo>> items =
        new List<KeyValuePair<string, MethodInfo>>();

    foreach (MethodInfo method in methods)
    {
        token = Attribute.GetCustomAttribute(method,
            typeof(MyToken), false) as MyToken;
        if (token == null)
            continue;

        items.Add(new KeyValuePair<String, MethodInfo>(
            token.DisplayName, method));

    }

    testListBox.DataSource = items;
    testListBox.DisplayMember = "Key";
    testListBox.ValueMember = "Value";
}

public void GetTest()
{//The next two methods handle selected value of the listbox and invoke …
Run Code Online (Sandbox Code Playgroud)

c# reflection multithreading

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

使用out参数反映静态重载方法

我有一些问题,通过反射调用带有out参数的重载静态方法,并会欣赏一些指针.

我期待动态地创建类型像System.Int32或者System.Decimal,然后调用静态TryParse(string, out x)方法就可以了.

以下代码有两个问题:

  • t.GetMethod("TryParse", new Type[] { typeof(string), t } ) 无法返回我期望的MethodInfo

  • mi.Invoke(null, new object[] { value.ToString(), concreteInstance })似乎成功但没有将out参数concreteInstance设置为已解析的值

交织到此函数中,您可以看到一些临时代码,演示如果type参数设置为应该发生什么System.Decimal.

public static object Cast(object value, string type)
{
    Type t = Type.GetType(type);
    if (t != null)
    {
        object concreteInstance = Activator.CreateInstance(t);
        decimal tempInstance = 0;

        List<MethodInfo> l = new List<MethodInfo>(t.GetMethods(BindingFlags.Static | BindingFlags.Public));

        MethodInfo mi;
        mi = t.GetMethod("TryParse", new Type[] { typeof(string), t } );  //this …
Run Code Online (Sandbox Code Playgroud)

c# reflection static-methods

14
推荐指数
1
解决办法
4544
查看次数

调用私有方法保留调用堆栈

我正试图找到"打破非公开方法"的解决方案.

我只是想调用RuntimeMethodInfo.InternalGetCurrentMethod(...),传递我自己的参数(所以我可以实现GetCallingMethod()),或直接RuntimeMethodInfo.InternatGetCurrentMethod(ref StackCrawlMark.LookForMyCaller)在我的日志记录例程中使用.GetCurrentMethod实施方式如下:

[MethodImpl(MethodImplOptions.NoInlining)] 
public static MethodBase GetCurrentMethod() 
{     
    StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;     
    return RuntimeMethodInfo.InternalGetCurrentMethod(ref lookForMyCaller); 
}
Run Code Online (Sandbox Code Playgroud)

InternalGetCurrentMethod声明在哪里:内部:-).

我使用反射调用方法没有问题,但这会弄乱调用堆栈,这只是必须要保留的一件事,否则它会破坏它的目的.

我的堆栈跟踪保持接近原始的几率是多少(至少在允许的StackCrawlMarks 的距离内,这是LookForMe,LookForMyCaller和LookForMyCallersCaller.是否有一些复杂的方法来实现我想要的?

c# reflection methods private

14
推荐指数
1
解决办法
2317
查看次数

如何在不知道封闭泛型类型的情况下访问通用属性

我有一个通用类型如下

public class TestGeneric<T>
{
    public T Data { get; set; }
    public TestGeneric(T data)
    {
        this.Data = data;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我现在有一个对象(来自某个外部源)我知道它的类型是一些封闭的TestGeneric <>,但我不知道TypeParameter T.现在我需要访问我的对象的数据.问题是我无法强制转换对象,因为我不确切知道哪个封闭的TestGeneric.

我用

// thx to http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class
private static bool IsSubclassOfRawGeneric(Type rawGeneric, Type subclass)
{
    while (subclass != typeof(object))
    {
        var cur = subclass.IsGenericType ? subclass.GetGenericTypeDefinition() : subclass;
        if (rawGeneric == cur)
        {
            return true;
        }
        subclass = subclass.BaseType;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

为了确保,我的对象是泛型类型.有问题的代码如下:

public static void Main()
{
    object myObject = new TestGeneric<string>("test"); // or from another …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection casting

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

如何通过反射获取委托类型的返回类型?

我正在为个人项目进行反思繁重的工作,我需要通过其Type对象访问委托的返回类型.这是一个小元,所以这是一个例子.

Type type = typeof(Func<Foo, Bar, Baz>);
// ????
// Use reflection to come to the following expected result
Type result = typeof(Baz);
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

我将不会有任何类型的实例进入Delegate.

c# reflection

13
推荐指数
1
解决办法
3095
查看次数

检查对象是否满足通用参数约束

我有一个类似于下面的界面:

public interface IInterface<T>
    where T : IInterface<T>
{
}
Run Code Online (Sandbox Code Playgroud)

现在我需要使用反射创建一个表示此接口的类型,例如

typeof(IInterface<>).MakeGenericType(someType);
Run Code Online (Sandbox Code Playgroud)

但是,我实际上并不知道什么类型的'someType'将在运行时之前,并且该类型可能无法作为通用接口的类型参数有效,因此MakeGenericType失败.

问题是,如何检查'someType'是否对泛型约束有效?

c# generics reflection

13
推荐指数
2
解决办法
3595
查看次数