简体   繁体   English

通过反思访问看似公共的财产

[英]Accessing a seemingly public property through reflection

Good day all, 美好的一天,

I am struggling to determine what's the issue with an attempt to access a public property on a class. 我正在努力确定尝试访问类上的公共属性的问题。

My need is very basic. 我的需要非常基本。 I have a public class that's correctly instanced in my routine, and I know, thanks to reflector, that this class has a property that I need to reference. 我有一个在我的例程中正确实例化的公共类,我知道,由于反射器,这个类有一个我需要引用的属性。

Problem is, the property is defined as thus: 问题是,属性定义如下:

public Vector3 root {
    [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get;
    [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] set;
}

The problem I'm facing is that all my attempts at getting the property simply fail. 我面临的问题是我获得财产的所有尝试都失败了。 I've instanced the Type, and tried with all possible combinations of binding flags 我已经实例化了Type,并尝试了所有可能的绑定标志组合

Type vtype = myobj.getType()
PropertyInfo[] vproperties;
vproperties = vtype.GetProperties();//(BindingFlags.Default | BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic);
for (int vpropertycounter =0 ; vpropertycounter < vproperties.Length ; vpropertycounter++) {
    Console.write( varbodyproperties[varpropertycounter].Name); <= 'root' never appears in this list
}

My suspicion and doubt revolves around the fact that the root property might not be 'visibile' because its getter and setter are 'wrapperless' and 'internal'. 我的怀疑和怀疑围绕着这样一个事实:root属性可能不是'visibile',因为它的getter和setter是'wrapperless'和'internal'。 Sadly I don't know if this can be overcome or not. 可悲的是,我不知道这是否可以克服。

I would like to know if this property can by all means be accessed, and possibly how to do it. 我想知道是否可以通过各种方式访问​​此属性,以及可能的方法。

All feedback is welcome. 欢迎所有反馈。

Thanks in advance. 提前致谢。

NOTE: addressing the property directly, as in "myobj.root" generates a compiler error for unknown property. 注意:直接寻址属性,如“myobj.root”为未知属性生成编译器错误。

Methods with MethodImplOptions.InternalCall are usually internal framework methods. MethodImplOptions.InternalCall方法通常是内部框架方法。 You cannot call them directly or via reflection (which is more or less the same thing). 你不能直接或通过反射(这或多或少是相同的东西)来调用它们。

It depends on the library, I found something like this within the Word API where it internally uses VB. 这取决于库,我在内部使用VB的Word API中找到了这样的东西。 To access variant Properties you'll have to call a setter method for example 要访问变体属性,您必须调用setter方法

 Property = "" <- doesn't work
 set_Property("") <- works

Depends on the API you are trying to access I guess, if those successors are implemented 取决于您尝试访问的API,我想,如果实现了这些后继者

Apart from that, maybe read the Platform Invoke Tutorial 除此之外,可以阅读平台调用教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM