为了账号安全,请及时绑定邮箱和手机立即绑定

针对不同用途使用 KeyValue DTO 的最佳实践是什么

针对不同用途使用 KeyValue DTO 的最佳实践是什么

C#
天涯尽头无女友 2023-08-20 15:56:39
如果您希望有两个不同类型的 KeyValue DTO(有时是 'int' 类型,有时是 'string' 类型),那么最佳实践是什么。有两个这样的 DTO 是不是更好:public class KeyValueDto    {        public int Key { get; set; }        public object Value { get; set; }    }public class KeyValueDto    {        public string Key { get; set; }        public object Value { get; set; }    }或者有一个 DTO:public class KeyValueDto    {        public object Key { get; set; }        public object Value { get; set; }    }考虑铸造操作的成本在哪里使用DTO?
查看完整描述

1 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

using System;


public class Program

{

    public static void Main()

    {

        var a= new KeyValueDto<int>(){

            Key = 1,

            Value = "ali"

        };


        var b = new KeyValueDto<string>{

            Key = "1",

            Value = "ali1"

        };


    }


    public class KeyValueDto<T>

    {

        public T Key { get; set; }

        public object Value { get; set; }

    }



}


查看完整回答
反对 回复 2023-08-20
  • 1 回答
  • 0 关注
  • 65 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信