简体   繁体   English

.NET Class属性作为对象,例如System.String

[英].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. 我正在创建要与脱机应用程序一起使用的新webAPI。 The app connects to the api (before going offline) and pulls down a list of fields to generate a dynamic form. 该应用程序连接到api(离线之前),并下拉字段列表以生成动态表单。 I want to supply a list of fields to the client. 我想向客户提供字段列表。 In simple terms this might be id and description . 简单来说,这可能是iddescription

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 . 但是,要完全按照我的要求生成表单,我可能要提供一个字段“类型”,例如System.DateTimeSystem.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 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. 这是我想能够实现的Type = System.DateTime 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 : 要表示类型 ,请使用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. 由于Type在平台之间不可互换,因此不太可能与webapi一起使用。

Another option is an enum of your expected types; 另一个选择是您期望的类型的enum you might find that TypeCode has everything you want, but you could also simply create your own enum of known types. 您可能会发现TypeCode具有所需的一切,但您也可以简单地创建自己的已知类型的enum

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSON.NET将System.Object属性的类型自动更改为System.String - JSON.NET changes type of a System.Object property automatically to System.String 无法将 System.String 强制转换或转换为 Class 对象 - Could not Cast or Convert System.String to Class object 无法将类型类的对象强制转换为“ System.String”类型 - Unable to cast object of type class to type 'System.String' ASP.NET FormView:“类型'System.Int32'的对象不能转换为类型'System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” Object to System.String隐式转换 - Object to System.String implicit conversion 无法将System.String转换为类类型 - Cannot convert System.String to class type 无法将“System.String”类型的 object 转换为“System.Boolean.Net Core MVC” - Unable to cast object of type 'System.String' to type 'System.Boolean .Net Core MVC asp.net错误:无法将'System.DateTime'类型的对象强制转换为'System.String' - asp.net error: Unable to cast object of type 'System.DateTime' to type 'System.String' 无法将“System.DBNull”类型的对象转换为 Scaffold-DbContext .Net Core 中的“System.String”类型 - Unable to cast object of type 'System.DBNull' to type 'System.String' in Scaffold-DbContext .Net Core 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM