猿问

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

如果您希望有两个不同类型的 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?


天涯尽头无女友
浏览 105回答 1
1回答

繁花不似锦

using System;public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var a= new KeyValueDto<int>(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key = 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "ali"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var b = new KeyValueDto<string>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key = "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "ali1"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; public class KeyValueDto<T>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public T Key { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public object Value { get; set; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答