简体   繁体   中英

.NET Class property as object, such as System.String

I'm not sure if this is possible, or even how to properly word this.

I am creating a new webAPI that I want to use with an offline app. The app connects to the api (before going offline) and pulls down a list of fields to generate a dynamic form. I want to supply a list of fields to the client. In simple terms this might be id and description .

However, to generate the form exactly as I want it, I might want to supply a field "type" such as System.DateTime or System.String . Is it possible to do this?

In pseudocode this might be:

public class EntityType
{
    public Guid EntityTypeId { get; set; }
    public string Description { get; set; }
    public Object Type { get; set; }
}

I might then want to create an EntityType like this:

EntityType entityType = new EntityType { 
    EntityTypeId = Guid.NewGuid(),
    Description = "Visit Date",
    Type = System.DateTime
};

It's the Type = System.DateTime I want to be able to achieve. I can then have my form generate a field where the type is date. The same might apply for strings for example.

To represent a type , use Type :

 public Type Type { get; set; }

...

Type = typeof(DateTime)

However! It is unlikely that this will work with webapi, since Type is not interchangeable between platforms.

Another option is an enum of your expected types; you might find that TypeCode has everything you want, but you could also simply create your own enum of known types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM