我在我的C#程序中使用了一个CLR的外部库,外部库里有个类是这样的:
public value class SubSetInfo
{
public:
UINT PrimType;
UINT VertexStart;
UINT IndexStart;
UINT PrimCount;
};
由于无法修改这个CLR库的代码,而我的程序里,在一些关键问题上使用了Class和Field的Custom Attribute机制,因此我在C#里面想为这个类和这个类下面的4个Field分别添加一些Attribute。
类我用的是TypeDescriptor:
            TypeDescriptor.AddAttributes(typeof(GCRenderFunctional.SubSetInfo), new AutoSerialClassAttribute());但是对于下面的四个Field该怎么增加一些新的Attribute呢?
如果不能增加,那么还有什么好点的方法可以实现这个目的么?

解决方案 »

  1.   

    你写一个类,继承SubSetInfo,然后里面添加自己需要的一些属性即可。PS:我从未见过class关键字前面可以用value修饰的写法,你真有创意。
      

  2.   

    唉,您说的方法我想到了,但就是不想这样做才想问问有什么好办法没呢。BTW:那是CLR代码……value class等同于C#中的struct
    ref class等同于C#中的class
      

  3.   


    struct不能继承啊,class还是继承来的好些.
      

  4.   

    最后没办法了,自己用string做了一套类似Attribute的机制,虽然麻烦但是好在解决问题了。
                TypeDescriptor.AddAttributes(
                    typeof(GCRenderFunctional.SubSetInfo),
                    new AutoSerialClassAttribute (
                        new string[]
                        {
                            "PrimType",
                            "VertexStart",
                            "IndexStart",
                            "PrimCount"
                        }
                        )
                    );
    解析的时候也用TypeDescriptor反解            AttributeCollection ac = TypeDescriptor.GetAttributes(type);
                AutoSerialClassAttribute asc = null;
                foreach (Attribute att in ac)
                {
                    asc = att as AutoSerialClassAttribute;
                    if (asc != null)
                    {
                        return asc;
                    }
                }
                return null;
      

  5.   

    呵呵,“CLR代码”!c++就c++呗,连大的名词(属于哪一个语言编译器)都搞错了,小的名词就肯定让人误会了。
      

  6.   

    呵呵,Sorry,多谢批评,我们公司不是专门用.NET的公司,口头上都这么叫,我也就脱口而出了。
    TypeDescriptor还是很强大的啊。
    就此结贴吧,需求算是满足了。