Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an alias for a property

I want to generate aliases for properties in generated code. All I could do so far was:

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

        set
        {
            this.User = value;
        }
    }
}

I wonder if there is any other way to define an alias in C#. The Purchase class was generated by Linq-to-SQL

like image 473
Jader Dias Avatar asked May 23 '12 20:05

Jader Dias


People also ask

What is an alias property?

The Alias property specifies an alternative name, an alias, for the name of a persistent type. By default, the short name of the type without its namespace is used. Alias names are convenient for specifying classes in OQL queries.

Why would someone have an alias name?

An alias can be any name used in place of a birth name. While there may be legitimate reasons for using another name, this is often used by criminals, as they don't wish to have their true identity revealed, or they have a short nickname they go by instead of their birth name.


2 Answers

In the case that you want a different name of property to send information to JSON using newtonSoft, you can use

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

This can help you if you don't want that your object follow the C# convention and match with the JSON object to be received or sent

like image 169
freedeveloper Avatar answered Sep 28 '22 22:09

freedeveloper


No, it's not possible to do in C#. Property name is single identifier you can define for it.

Don't know if this is what you're searching for or not:

but you can define (say) a Dictionary<string,object> where Key is a propertyname and value is a value of the property. In this way you can define dynamic property cash, with changing property names and values at runtime.

or can use an ExpandoObject , if you use C# 4.0

like image 30
Tigran Avatar answered Sep 28 '22 21:09

Tigran