c#中实现的万能变量类
发布日期:2022-03-03 10:44:07
浏览次数:6
分类:技术文章
本文共 7159 字,大约阅读时间需要 23 分钟。
c#里面有一个很好的机制一切对象都继承自Object
废话少说直接上代码:第一个类Varient.cs文件
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 公用的容器
/// </summary>
public class Variant
{
public bool isNull
{
get{return m_value == null;}
set{if (value == true){m_value = null;}}
}
public void set_bool(bool b)
{m_value = b;}
public void set_value(double b)
{m_value = b;}
public void set_int(int b)
{ m_value = b; }
public void set_string(string b)
{m_value = b;}
public bool get_bool()
{
if (m_value == null || m_value.GetType() != typeof(bool))
{return false;}
return (bool)m_value;
}
public double get_value()
{
if (m_value == null || m_value.GetType() != typeof(double))
{return 0;}
return (double)m_value;
}
public int get_int()
{
if (m_value == null || m_value.GetType() != typeof(int))
{ return 0; }
return (int)m_value;
}
public string get_string()
{
if (m_value == null)
{return "";}
return m_value.ToString();
}
public void set_object(object a)
{m_value = a;}
public void set_json(string data)
{
LitJson.JsonData js = LitJson.JsonMapper.ToObject(data);
try
{
_json_add(js, this);
}
catch (System.Exception ex)
{
Logging.LogError(ex.ToString());
m_value = null;
}
}
public string to_json()
{
LitJson.JsonData js = _json_get(this);
if (js == null){return "{}";}
return js.ToJson();
}
static void _json_add(LitJson.JsonData js, Variant v)
{
if (js.IsObject){_json_add_obj(js, v);}
else if (js.IsArray){_json_add_list(js, v);}
else if (js.IsBoolean){_json_add_bool(js, v);}
else if (js.IsInt){_json_add_int(js, v);}
else if (js.IsDouble){_json_add_double(js, v);}
else{_json_add_string(js, v);}
}
static void _json_add_obj(LitJson.JsonData js, Variant v)
{
Dictionary<string, Variant> map = v.to_name_map();
if (map == null)
{return;}
foreach (string vk in ((IDictionary)js).Keys)
{
LitJson.JsonData js_s = js[vk];
map[vk] = new Variant();
_json_add(js_s, map[vk]);
}
}
static void _json_add_list(LitJson.JsonData js, Variant v)
{
var list = v.to_list();
for(int i=0; i < js.Count; i++)
{
var sub = new Variant();
list.Add(sub);
_json_add(js[i], sub);
}
}
static void _json_add_bool(LitJson.JsonData js, Variant v)
{
v.set_bool((bool)js);
}
static void _json_add_int(LitJson.JsonData js, Variant v)
{
v.m_value = ((int)js);
}
static void _json_add_double(LitJson.JsonData js, Variant v)
{
v.m_value = ((double)js);
}
static void _json_add_string(LitJson.JsonData js, Variant v)
{
v.m_value = js.ToString();
}
static LitJson.JsonData _json_get(Variant v)
{
if (v.m_value == null)
{
return null;
}
if (v.m_value.GetType() == typeof(Dictionary<string, Variant>))
{
LitJson.JsonData js = new LitJson.JsonData();
foreach(var kv in ((Dictionary<string, Variant>)v.m_value))
{
js.SetJsonType(LitJson.JsonType.Object);
((IDictionary)js)[kv.Key] = _json_get(kv.Value);
}
return js;
}
else if (v.m_value.GetType() == typeof(List<Variant>))
{
LitJson.JsonData js = new LitJson.JsonData();
js.SetJsonType(LitJson.JsonType.Array);
foreach (var kv in ((List<Variant>)v.m_value))
{
js.Add(_json_get(kv));
}
return js;
}
else if (
v.m_value.GetType() == typeof(int) ||
v.m_value.GetType() == typeof(double) ||
v.m_value.GetType() == typeof(bool))
{
return new LitJson.JsonData(v.m_value);
}
else
{
return new LitJson.JsonData(v.m_value.ToString());
}
}
public object get_object(string type)
{
Type t = Type.GetType(type);
if (t == null){return null;}
if (m_value == null){return null;}
if (m_value.GetType() != t){return null;}
return m_value;
}
public string to_string()
{
if (m_value == null){return "";}
return m_value.ToString();
}
public double to_double(double def)
{
string sv = to_string();
double ret = def;
if (double.TryParse(sv, out ret))
{return ret;}
return def;
}
public float to_float(float def)
{
return (float)to_double((double)def);
}
public int to_int(int def)
{
return (int)to_double((double)def);
}
public object[] list_to_params()
{
if (m_value == null || m_value.GetType() != typeof(List<Variant>))
{return new object[0];}
var l = to_list();
if (l == null || l.Count == 0)
{return new object[0];}
object[] pl = new object[l.Count];
for (int i = 0; i < l.Count; i++)
{pl[i] = l[i].m_value;}
return pl;
}
public List<Variant> to_list()
{
if(m_value != null && m_value.GetType() == typeof(List<Variant>))
{return (List<Variant>)m_value;}
List<Variant> l = new List<Variant>();
m_value = l;
return l;
}
public Dictionary<int, Variant> to_map()
{
if(m_value != null && m_value.GetType() == typeof(Dictionary<int,Variant>))
{return (Dictionary<int, Variant>)m_value;}
Dictionary<int, Variant> l = new Dictionary<int, Variant>();
m_value = l;
return l;
}
public Dictionary<string, Variant> to_name_map()
{
if (m_value != null && m_value.GetType() == typeof(Dictionary<string, Variant>))
{return (Dictionary<string, Variant>)m_value;}
Dictionary<string, Variant> l = new Dictionary<string, Variant>();
m_value = l;
return l;
}
public Variant push_in_list()
{
var n = new Variant();
to_list().Add(n);
return n;
}
public Variant get_in_list(int id)
{
List<Variant> v = to_list();
if (v == null)
{
return null;
}
if (id < 0 || id >= v.Count)
{
return null;
}
return v[id];
}
public Variant apply_in_map(int id)
{
var v = to_map();
if (!v.ContainsKey(id))
{v[id] = new Variant();}
return v[id];
}
public Variant apply_in_name_map(string n)
{
var v = to_name_map();
if (!v.ContainsKey(n))
{ v[n] = new Variant(); }
return v[n];
}
public override string ToString()
{
if (m_value == null)
{ return "Variant:null"; }
return "Variant:" + m_value.GetType().Name + ":" + m_value.ToString();
}
object m_value = null;
}
//第二个类文件 cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace vs2015demo
{
public class Person
{
public string Name { set; get; }
public int Age { set; get; }
public string Birthday { set; get; }
}
class Program
{
static void Main(string[] args)
{
Variant my = new Variant();
string json = @"
{
""Name"" : ""YANGJIE"",//测试了下单引号和双引号都可以
'Age' : 24,
'Birthday' : '1990'
}";
string json2 = @"
{
""Name"" : ""WUGANG"",
'Age' : 25,
'Birthday' : '1990'
}";
//Person per = LitJson.JsonMapper.ToObject<Person>(json);
//LitJson.JsonData js = LitJson.JsonMapper.ToObject(json);
//Console.WriteLine(js["Name"].ToString() + js["Age"].ToString() +
// js["Birthday"].ToString());
string[] arrstr = { "yangjie", "wugang" };
string[] jsonstr = { json, json2 };
for (int i = 0; i < arrstr.Length; i++)
{
my.apply_in_name_map(arrstr[i]).set_json(jsonstr[i]);
}
//my.apply_in_map("yangjie").set_json(json);
//my.apply_in_map("wugang").set_json(json2);
for (int i = 0; i < arrstr.Length; i++)
{
var it = my.apply_in_name_map(arrstr[i]);
Console.WriteLine(it.to_name_map()["Name"].get_string());
Console.WriteLine(it.to_name_map()["Age"].get_int());
Console.WriteLine(it.to_name_map()["Birthday"].get_string());
}
// my.apply_in_name_map("yangjie").set_int(25);
// var age = my.apply_in_name_map("yangjie").get_int();
//my.push_in_list();
//my.push_in_list();
//my.get_in_list(0).set_int(100);
//my.get_in_list(1).set_int(101);
//for (int i = 0; i < 2; i++)
//{
// Console.WriteLine(my.get_in_list(i).get_int());
//}
// Console.WriteLine(age);
Console.ReadKey();
}
}
}
转载地址:https://blog.csdn.net/yangjie6898862/article/details/52245856 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月19日 17时16分48秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP字符串运算结果,PHP运算符(二)"字符串运算符"实例详解
2019-04-21
PHP实现 bcrypt,如何使php中的bcrypt和Java中的jbcrypt兼容
2019-04-21
php8安全,PHP八大安全函数解析
2019-04-21
php基础语法了解和熟悉的表现,PHP第二课 了解PHP的基本语法以及目录结构
2019-04-21
matlab中lag函数用法,MATLAB movavg函数用法
2019-04-21
matlab变形监测,基于matlab的变形监测数据处理与分析_毕业设计论文
2019-04-21
opencv matlab编程,在Matlab中调用OpenCV函数 | 学步园
2019-04-21
c语言文件wt,c语言,wt和rt中的t是什么意思
2019-04-21
c语言运行几进制,【C语言】求已知等式在几进制条件下成立
2019-04-21
电梯运行仿真c语言代码,电梯调度算法模拟(示例代码)
2019-04-21
云麦小米华为体脂秤怎么样_云康宝和华为智能体脂秤对比评测,实际体验告诉你哪款更好...
2019-04-21
linux 条件判断 取非_Linux awk 系列文章之 awk 多重条件判断
2019-04-21
c语言中如何将字符串的元素一个一个取出_C语言中常用的字符串操作函数
2019-04-21