C#动态转换为字符串的最佳方式 [英] C# Best way to convert dynamic to string

查看:1614
本文介绍了C#动态转换为字符串的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面从数据表获得一个场并将其转换为字符串。 ?是否有动态转换为字符串一个更清洁的方式。



 动态值= dataTable.Rows [I]技术领域<动态>( COLUMNNAME); 
值=(价值== NULL)?空:value.ToString();


解决方案

 字符串值= Convert.ToString(dataTable.Rows [I] [COLUMNNAME]); 



标准格式将踢,而不需要的东西像仿制药,扩展方法是动态的。


The following gets a field from a DataTable and converts it to string. Is there a cleaner way to convert dynamic to string?

dynamic value = dataTable.Rows[i].Field<dynamic>(columnName);
value = (value == null) ? null : value.ToString();

解决方案

string value = Convert.ToString(dataTable.Rows[i][columnName]);

the standard formatting will kick in, without the need for things like generics, extension methods or dynamic.

这篇关于C#动态转换为字符串的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆