如何将GUID作为属性参数? [英] How do I take a GUID as an attribute parameter?

查看:49
本文介绍了如何将GUID作为属性参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像这样的某些属性类中的Guid属性:

I need a Guid property in some attribute class like this:

public class SomeAttribute : Attribute {
    private Guid foreignIdentificator;
    public Guid ForeignIdentificator {
        get { return this.foreignIdentificator; }
        set { this.foreignIdentificator = value; }
    }
}

但是在属性定义中,我只能使用基本类型,它们是常量(我理解为什么,这很有意义).解决方法可以是将"ForeignIdentificator"定义为字符串,然后在运行时创建Guid:

But in attribute definition I can use only primitive types, which are constants ( I understand why, and it's make me sense ). The workaround can be definition "ForeignIdentificator" as string, and creating Guid in runtime:

public class SomeAttribute : Attribute {
    private string foreignIdentificator;
    public string ForeignIdentificator {
        get { return this.foreignIdentificator; }
        set { this.foreignIdentificator = value; }
    }
    public Guid ForeignIdentificatorGuid {
        get { return new Guid( ForeignIdentificator ); }
    }
}

不好意思,我没有检查类型安全性."ForeignIdentificator"属性可以包含任何字符串值,并且在创建Guid期间将在运行时而不是在编译时引发异常.

Unahppily I loose check for type safety. The "ForeignIdentificator" property can contains any string value and during creating Guid will be thrown exception at runtime, not at compile time.

我知道编译器会检查"System.Runtime.InteropServices.GuidAttribute"的字符串值是否具有"Guid兼容性".这项检查正是我所需要的,但是我不知道该检查是我在编译器中进行硬编码还是可以明确定义(以及如何).

I know the compiler checks string value for "System.Runtime.InteropServices.GuidAttribute" for "Guid compatibility". This check is exactly what I need, but I don't know if this check i hardcoded in compiler or can me explicitly defined ( and how ).

您知道某种方式如何保护属性的向导兼容性"检查吗?还是另一种方式,如何在属性中达到类型安全的Guid定义?谢谢.

Do you know some way, how to secure "Guid compatibilty" check for attributes? Or some another way, how to reach type safe Guid definition in attributes? Thanks.

推荐答案

我过去遇到过您的确切问题.我们仅要求它们将GUID作为字符串传递... VS GUID生成器工具将其提供给我们的默认方式(* F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4).我们基本上做了您所做的.我们在插件体系结构中使用它,因此我们的客户一直是使用该界面的人.如果您查看Microsoft在需要执行相同操作时所执行的操作,那么他们就会执行此操作.

I have run into your exact problem in the past. We simply required them to pass in the GUID as a string... the default way that the VS GUID generator tool gives it to us (*F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4). We essentially did what you did. We use it in a plug-in architecture, so our customers have been the ones to use the interface. If you look at what Microsoft does when they need to do the same thing, they do that.

以这种方式进行并不是没有问题.我们一次都没有将它视为实地问题.

It hasn't been a problem to do it this way. Not once, have we seen this as an issue from the field.

不过,您可能要为该字符串字段GUID命名,以免混淆您的使用者.添加一些文档,以防他们不知道需要使用什么格式.

You might want to name that string field GUID, though, as to not confuse your consumers. Add some documentation in case they don't know what format it needs to be in.

当我看着这个时,我有同样的反应...但是后来我继续前进,因为似乎没有类型安全的解决方案.

I had the same reaction when I looked at this... but then I just moved on, as it seems there is no type-safe solution.

这篇关于如何将GUID作为属性参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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