English 中文(简体)
如何界定财产的别名
原标题:How to define an alias for a property

我想在生成代码中生成属性别名。

partial class Purchase
{
    public User Customer
    {
        get
        {
            return this.User;
        }

        set
        {
            this.User = value;
        }
    }
}

我想知道 C# 中是否有其它方法定义别名 。 < code> Purchase 类是由 Linq- to- SQL 生成的

最佳回答

无法在 C # 中完成。 属性名称是您可以定义的单一标识符 。

不知道这是不是你们要找的?

但您可以定义( says) 一种 < code> dictionary< string; a/ object> , 其中 < code > Key 是 < em > propertyname value 是该属性的价值。 这样您就可以定义动态属性现金, 在运行时间 < / em > 时更改属性名称和值 < em > 。

或使用 < a href=> http://msdn.microsoft.com/ en- us/library/ system. extandobject.aspx" rel = "nofollow" > ExpandoObject ,如果使用 C# 4.0

问题回答

如果您想要用不同的财产名称使用牛tonSoft 将信息发送给JSON, 您可以使用

 [JsonProperty("alias_name")]
 public type YourProperty {get;set;}

如果您不希望您的对象遵循 C # 常规, 并符合 JSON 目标被接收或发送, 这样可以帮助您 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...