是否可以在C#中使用别名数组类型? [英] Is it possible to alias array type in c#?

查看:77
本文介绍了是否可以在C#中使用别名数组类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义异常类型进行序列化/反序列化。此类型的字段定义为:

I'm trying to do some serialization / deserialization stuff with a custom exception type. This type has a field defined as:

private object[] resourceMessageParams;

我有一些不错的强类型代码,并带有一些Linq表达式魔术,但是我想去

I've got all nice and strongly typed code with some Linq expression magic, but I want to go even further than that and do something like this:

using ResourceMessageParamsType = object[];//<-- "Identifier expected" error here
/*...*/
private ResourceMessageParamsType resourceMessageParams;
/*...*/
this.resourceMessageParams = 
    (ResourceMessageParamsType)serializationInfo.GetValue(
        ReflectionHelper.GetPropertyNameFromExpression(() => 
            resourceMessageParams), typeof(ResourceMessageParamsType));

代替此:

(object[])serializationInfo.GetValue(
    ReflectionHelper.GetPropertyNameFromExpression(() => 
        resourceMessageParams), typeof(object[]));

为了适应将来可能更改此字段的类型,因此必须更改在别名定义中仅输入一次。但是,编译器使用ResourceMessageType = object []在中的 object 处停止; 抱怨期望使用标识符。更改为 Object [] 会有所帮助,但是这次用相同的错误消息突出显示了括号。

To accomodate for possible change of type of this field in the future, so one will have to change the type only once in alias definition. However, the compiler stops at object in using ResourceMessageType = object[]; complaining that an identifier is expected. Changing to Object[] helps somewhat, but this time the bracket is highlighted with the same error message.

是有没有一种方法可以为c#中的数组类型定义别名?

Is there a way to define an alias for array type in c#?

推荐答案

简单地说,您不能为数组类型别名

Simply put, you can't "alias" array types.

您可以通过将内容封装在 struct 中来解决该问题,但这并不能回答您的问题。

You can work around it by encapsulating things in a struct, but that doesn't answer your question.

更新:

根据ECMA标准,


使用别名指令:

使用 标识符 = 命名空间或类型名称;

显然没有说明允许的数组。

which clearly doesn't say anything about arrays being permitted.

(有关如何 namespace-or-type-name 已定义。)

这篇关于是否可以在C#中使用别名数组类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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