是否有可能在C#中从该财产的实例中得到连接到财产的属性,不知道包含类? [英] Is it possible in c# to get the attributes attached to a property from within the instance of that property, without knowing the containing class?

查看:85
本文介绍了是否有可能在C#中从该财产的实例中得到连接到财产的属性,不知道包含类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题有点拗口,但它更容易在code看到:

The title is a bit of a mouthful, but it's easier to see in code:

public struct MyStruct {
    public bool HasAttribute(Attribute attribute) {
        //is there any way to know?
        return ??????;
    }
}

public class MyClass {
    [SomeAttribute]
    MyStruct child;

    public MyClass() {}
}

我已经知道如何通过让每个其属性的属性信息,然后调用 GetCustomAttributes MyClass.child 属性code>,但这只是工作,如果我知道的结构实例对应于 MyClass.child 。我想在这里做的是找出如果一个结构的特定实例连接到它的属性,不知道什么类包含特定实例。

I already know how to find the attributes on MyClass.child by getting the property info for each of its properties and then calling GetCustomAttributes, but this only works if I know that the struct instance corresponds to MyClass.child. What I'd like to do here is to figure out if a particular instance of a struct has attributes attached to it, without knowing what class contains the particular instance.

这将是有意义的我,如果你不能做到这一点,以供参考类型,因为实例可以从多个地方被引用,但不应属性集始终值类型定义良好的?

It would make sense to me if you couldn't do this for reference types because the instance could be referenced from multiple places, but shouldn't the set of attributes always be well-defined for value types?

我的使用情况作出,其中某些对象的行为,可以通过将自定义属性稍微修改的库。如果有解决这个更习惯的方法,然后我打开的建议。我也愿意进入一个不安全的情况下,如果允许一个解决方案。

My use case is making a library where the behavior of certain objects can be modified slightly by attaching custom attributes. If there's a more idiomatic way to approach this then I'm open to suggestions. I'm also willing to go into an unsafe context if that allows for a solution.

推荐答案

抱歉,但这是不可能的。 [SomeAttribute] 连接到 MyClass的中的字段的,而且是完全无关的 MYSTRUCT

Sorry, but that's not possible. [SomeAttribute] is attached to the field of MyClass, and is completely unrelated to MyStruct.

有没有办法让一个结构的的容器的一对夫妇的原因:

There's no way to get the container of a struct for a couple of reasons:


  • 有可能仅仅是一个局部变量或临时的,这意味着该结构既可以住在堆栈或在CPU寄存器。因此,无论有没有包含的类存在。

  • It could simply be a local variable or a temporary, which means the struct could either live on the stack or in a CPU register. So, no there's no containing class there.

它可以被包含在另一个结构,这将受到同样的问题。

It could be contained in another struct, which would be subject to the same issue.

如果它包含在一个类,你必须检索仅使用结构的地址容器类型。这将涉及非常讨厌code,如果可能的话都没有。

If it's contained in a class, you'd have to retrieve the container type using only the struct's address. And that would involve very nasty code, if possible at all.

试想:这种结构的地址将是其包含类加上偏移地址。你必须找到某种容器的虚函数表的地址(假设你可以推断什么看起来像一个虚函数表刚刚从一个内存地址)。这将是完全有害/不安全​​的的不可靠,你会得到运行的访问冲突无时无刻的风险,因为你不得不取消引用未知的地址。这里还有没办法的得到的东西可靠出来。

Just imagine: The address of such a struct would be the address of its containing class plus an offset. You'd have to somehow find the address of the container's vtable (assuming you could infer what looks like a vtable just from a memory address). This would be totally hazardous/unsafe and unreliable, you'd run the risk of getting access violations all the time since you'd have to dereference unknown addresses. There's just no way to get something reliable out of it.

呵呵,你也不得不这样做的,而的GC的可能四处移动你的容器对象,没有牵制它的可能性,因为你没有在第一时间知道自己的地址

Oh, and you'd also have to do this while the GC could potentially move your container object around, without the possibility of pinning it since you don't know its address in the first place.

一个理智的方法是使用CLR调试API,但是在这一点上我假设你刚刚的不想召唤邪神

A saner approach would be to use the CLR debugging API, but at this point I'll assume you just don't want to summon Cthulhu.

我敢肯定有很多其他的方式来达到你想要有什么,但我不能没有更多的细节更加具体。现在,我建议明确初始化你的结构,与其他参数:

I'm sure there are many other ways to achieve what you want there, but I can't be more specific without more details. Right now, I'd suggest initializing your struct explicitly, with additional parameters:

public class MyClass {
    MyStruct child = new MyStruct(doThatSpecialThingie: true);
}

这篇关于是否有可能在C#中从该财产的实例中得到连接到财产的属性,不知道包含类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆