标签: reflection

memberInfo.GetValue()C#

如何获取实例成员的值?

对于propertyInfos,有一个propertyInfo.GetValue(instance, index),但memberInfo中不存在这样的东西.

我在网上搜索,但似乎停止获取会员的姓名和类型.

c# reflection

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

获取类属性名称

我有我的winform应用程序使用数据绑定收集数据.一切都很好,除了我必须使用字符串链接属性与textedit:

Me.TextEdit4.DataBindings.Add(New System.Windows.Forms.Binding("EditValue",Me.MyClassBindingSource,"MyClassProperty",True))

这工作正常,但如果我更改类的属性名称,编译器显然不会警告我.

我希望能够通过反射获取属性名称,但我不知道如何指定我想要的属性的名称(我只知道如何在类的所有属性之间进行迭代)

任何的想法?

.net reflection

11
推荐指数
2
解决办法
7235
查看次数

无法将'System.Object []'类型的对象强制转换为'MyObject []',是什么给出的?

场景:

我正在编写一个层来将3个类似的Web服务抽象为一个可用的类.每个Web服务都公开一组共享共性的对象.我创建了一组利用共性的中间对象.但是在我的图层中,我需要在Web服务对象和我的对象之间进行转换.

在调用Web服务之前,我已经使用反射在运行时创建了相应的类型,如下所示:

    public static object[] CreateProperties(Type type, IProperty[] properties)
    {
        //Empty so return null
        if (properties==null || properties.Length == 0)
            return null;

        //Check the type is allowed
        CheckPropertyTypes("CreateProperties(Type,IProperty[])",type);

        //Convert the array of intermediary IProperty objects into
        // the passed service type e.g. Service1.Property
        object[] result = new object[properties.Length];
        for (int i = 0; i < properties.Length; i++)
        {
            IProperty fromProp = properties[i];
            object toProp = ReflectionUtility.CreateInstance(type, null);
            ServiceUtils.CopyProperties(fromProp, toProp);
            result[i] = toProp;
        }
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

这是我的调用代码,来自我的一个服务实现:

Property[] props …
Run Code Online (Sandbox Code Playgroud)

.net c# arrays reflection casting

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

为什么这个属性Getter是虚拟的?

对某些C#代码有一个奇怪的问题 - 当没有明确标记时,属性的Getter方法显示为虚拟.

该问题在此类上显示DbKey属性(代码完整):

public class ProcessingContextKey : BusinessEntityKey, IProcessingContextKey
{
    public ProcessingContextKey()
    {
        // Nothing
    }

    public ProcessingContextKey(int dbKey)
    {
        this.mDbKey = dbKey;
    }

    public int DbKey
    {
        get { return this.mDbKey; }
        set { this.mDbKey = value; }
    }
    private int mDbKey;

    public override Type GetEntityType()
    {
        return typeof(IProcessingContextEntity);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我使用反射来检查DbKey属性时,我得到以下(意外)结果:

Type t = typeof(ProcessingContextKey);
PropertyInfo p = t.GetProperty("DbKey");
bool virtualGetter = p.GetGetMethod(true).IsVirtual; // True!
bool virtualSetter = p.GetSetMethod(true).IsVirtual; // False
Run Code Online (Sandbox Code Playgroud)

为什么virtualGetter设置为True?鉴于该属性既不是抽象的也不是虚拟的,我期望是假的.

为了完整性 …

.net c# reflection .net-3.5

11
推荐指数
3
解决办法
1783
查看次数

取出特定命名空间的所有类

有没有办法从特定的命名空间中获取对象?或许与System.Reflections?我希望将所有对象从ITestType命名空间中的类型Test.TestTypes作为对象获取,以便我有一个实例列表,TestType1, TestType2, TestType3依此类推.有人能帮我吗?我不知道在哪里搜索.

c# reflection namespaces

11
推荐指数
1
解决办法
4531
查看次数

是否有一个用于Java反射的通用"后端"库

我目前正在使用用Java实现的专门的,解释性的编程语言.作为该语言的一小部分,我想添加调用Java的能力.在深入研究反射的所有细节之前,我想知道是否有人知道一个通​​用库来执行反向调用Java代码的"后端"部分.

也就是说,我将一个字符串(我定义语法)解析为一些表示Java方法调用(或构造函数或字段访问)的数据结构,然后将该数据结构传递给调用该调用并返回结果的库.特别是,我希望它已经处理了我不想弄清楚的所有边缘情况:

  • 根据参数类型自动选择正确的方法(如智能Class.getDeclaredMethod())
  • 处理数组和普通对象引用之间的区别
  • 等等

我花了一点时间在JVM上查看动态语言的实现,但这些通常比我正在寻找的要复杂得多,或者针对特定语言进行了高度优化.

另一个选择是将我的语法转换为某种动态语言的字符串,并使用Rhino或其他东西调用它,但这比我正在寻找的开销稍微多一些.

谢谢!

java reflection dynamic invoke

11
推荐指数
3
解决办法
4575
查看次数

.NET反射:检测IEnumerable <T>

我正在尝试检测Type对象的特定实例是否是通用的"IEnumerable"...

我能想到的最好的是:

// theType might be typeof(IEnumerable<string>) for example... or it might not
bool isGenericEnumerable = theType.GetGenericTypeDefinition() == typeof(IEnumerable<object>).GetGenericTypeDefinition()
if(isGenericEnumerable)
{
    Type enumType = theType.GetGenericArguments()[0];
    etc. ...// enumType is now typeof(string) 
Run Code Online (Sandbox Code Playgroud)

但这似乎有点间接 - 是否有更直接/更优雅的方式来做到这一点?

.net generics reflection

11
推荐指数
1
解决办法
3991
查看次数

如何获取基类的所有继承类?

    class Foo { }

    class Foo1 : Foo { }

    class Foo2 : Foo { }
Run Code Online (Sandbox Code Playgroud)

我怎样才能将所有使用Foo的类作为基类?在同一程序集中不需要继承的类.

c# reflection inheritance assemblies

11
推荐指数
1
解决办法
6400
查看次数

从包中获取所有类

可以说我有一个java包commands,其中包含所有继承自的类,ICommand我能以某种方式得到所有这些类吗?我正在锁定以下内容:

Package p = Package.getPackage("commands");
Class<ICommand>[] c = p.getAllPackagedClasses(); //not real 
Run Code Online (Sandbox Code Playgroud)

有可能吗?

java reflection package

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

对象属性路径的 TypeScript 类型定义

是否可以以这样的方式键入字符串数组,使得该数组只能是给定对象中的有效属性路径?类型定义应该适用于所有深度嵌套的对象。

例子:

const object1 = {
    someProperty: true
};
const object2 = {
    nestedObject: object1,
    anotherProperty: 2
};

type PropertyPath<Type extends object> = [keyof Type, ...Array<string>]; // <-- this needs to be improved

// ----------------------------------------------------------------

let propertyPath1: PropertyPath<typeof object1>;

propertyPath1 = ["someProperty"]; // works
propertyPath1 = ["doesntExist"]; // should not work

let propertyPath2: PropertyPath<typeof object2>;

propertyPath2 = ["nestedObject", "someProperty"]; // works
propertyPath2 = ["nestedObject", "doesntExist"]; // should not work
propertyPath2 = ["doesntExist"]; // should not work
Run Code Online (Sandbox Code Playgroud)

链接到 TypeScript 游乐场

reflection recursion types typescript jsonpointer

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