1.首先需要静态类,和静态方法
2.参数需要this关键字、以及需要扩展的类型
这里以万物转JSON为列
/// <summary>
/// 将指定的对象序列化成 JSON 数据。
/// </summary>
/// <param name="obj">要序列化的对象。</param>
/// <returns></returns>
public static string ToJson(this object obj)
{
try
{
if (null == obj)
return null;
else
return JsonConvert.SerializeObject(obj, Formatting.None, srJsonSettings);
}
catch (Exception)
{
return null;
}
``` }