如何将元数据添加到System.Type? [英] How to add metadata to the System.Type?

查看:80
本文介绍了如何将元数据添加到System.Type?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一种语言,但是就.NET集成而言,到目前为止,我只能设法使原始类型起作用.昨晚我有一个好主意-而不是试图找出类型检查期间元组,模块和函数的确切System.Type应该是什么,我可以做的是获取System.ObjectSystem.Type并添加一些元数据这对应于我自己的复杂语言类型.重要的不是确切的System.Type我要向其中添加元数据,而是我的语言中每种类型的替身都彼此不同.

I've been working on a language, but in terms of .NET integration I've only managed to get primitive types working so far. Last night I had a good idea - instead of trying to figure out what the exact System.Type for tuples, modules and functions should be during typechecking, what I could do instead is get the System.Type of System.Object and add some metadata which corresponds to my own complex language types. What matters is not the exact System.Type I would be adding the metadata to, but that each of the stand-ins for the types in my language are distinct from each other.

我需要这个的原因是我正在使用反射名称空间中的函数进行集成,这涉及到杂乱地System.Type s.

The reason I need this is that I am using the functions from the reflection namespace for integration which involves juggling System.Types.

type SpiralType =
    | IntT
    | StringT
    | TupleT of SpiralType list

作为一个例子,我想添加到System.Type的是上述F#区分联合类型的实例,以确保唯一性.

As an example, what I would like to add to the System.Type would be the instances of the above F# discriminated union type in order to ensure uniqueness.

做到这一点的最佳方法是什么?

What would be the best way of doing this?

推荐答案

在.NET中用额外的数据扩展类型信息的标准方法是使用属性,尽管属性构造函数参数通常限于您可以表示为的值文字.对于您来说,这可能是问题,也可能不是问题,这取决于您的元数据将变得多么丰富. DU可能行不通.

A standard way of augmenting type information with extra data in .NET would be to use attributes, though attribute constructor arguments would normally be limited to values you can represent as literals. This may or might not be an issue for you, depending on how rich you would your metadata to be. DU's probably won't work.

否则,一种直接的解决方案是将您的值与任意元数据一起包装为通用类型,即

Otherwise, a straightforward solution would be to wrap your value together with arbitrary metadata into a generic type, i.e.

type TypeWithMetadata<'typ, 'metadata>(value: 'typ, metadata: 'metadata) = 
    member this.Value = value
    member this.Metadata = metadata

虽然您可能想避免这些开销?

though that might come with an overhead you'd want to avoid?

这篇关于如何将元数据添加到System.Type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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