asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

<p class="title">一、引入

  最近遇到一个项目里面的功能,在给实体类赋值的时候,由于赋值字段是动态生成的,所以如果用常用的方法(直接实体类的名称.字段名=要赋的值),将会生成很多无用的代码,所以找到了一个通过反射的赋值与取值的方法,顺便总结一下,以及对比一下与Python语言同样实现该功能的区别之处。

title">二、C#

title">  1.赋值

  

title="C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值" alt="C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值" src="https://cn.js-code.com/res/2019/02-06/17/bb2692fff79cf365d9e2efd2a26d775b.png" >

title">  2.取值

  

title="C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值" alt="C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值" src="https://cn.js-code.com/res/2019/02-06/17/5ca9d6a7f002e0cb11ec841a8178d1fa.png" >

title">  3.源码

https://cn.js-code.com/res/2019/02-06/17/1c53668bcee393edac0d7b3b3daff1ae.gif" alt="">https://cn.js-code.com/res/2019/02-06/17/405b18b4b6584ae338e0f6ecaf736533.gif" alt="">
m.Collections.Generic; m.Linq; m.Reflection; m.Text; m.Threading.Tasks;

<span style="color: #0000ff">namespace<span style="color: #000000"> ConsoleApplication1
{
<span style="color: #0000ff">class<span style="color: #000000"> Program
{
<span style="color: #0000ff">static <span style="color: #0000ff">void Main(<span style="color: #0000ff">String<span style="color: #000000">[] args)
{
<span style="color: #0000ff">#region 通过字符串设置实体类的值
<span style="color: #008000">//<span style="color: #008000">初始化一个实体类
<span style="color: #008000">//<span style="color: #008000">student model_stu = new student();
<span style="color: #008000">//<span style="color: #008000">String id_str = "stu_id";
<span style="color: #008000">//<span style="color: #008000">String name_str = "stu_name";
<span style="color: #008000">//<span style="color: #008000">String addr_str = "stu_address";
<span style="color: #008000">//<span style="color: #008000">Type type = model_stu.GetType();<span style="color: #008000">//<span style="color: #008000">获取类型
<span style="color: #008000">//<span style="color: #008000">PropertyInfo property_info_id = type.GetProperty(id_str);
<span style="color: #008000">//<span style="color: #008000">PropertyInfo property_info_name = type.GetProperty(name_str);
<span style="color: #008000">//<span style="color: #008000">PropertyInfo property_info_addr = type.GetProperty(addr_str);

        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;property_info_id.SETVALue(model_stu,5);
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;property_info_name.SETVALue(model_stu,"李四");
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;property_info_addr.SETVALue(model_stu,"北京市");

        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;Console.WriteLine(model_stu.stu_id);
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;Console.WriteLine(model_stu.stu_@R_616_8313@;
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;Console.WriteLine(model_stu.stu_address);
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;Console.ReadKey();</span>
        <span style="color: #0000ff"&gt;#endregion</span>

        <span style="color: #0000ff"&gt;#region</span> 通过字符串获取实体类的值
        <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;初始化一个实体类</span>
        student model_stu = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; student()
        {
            stu_id </span>= <span style="color: #800080"&gt;1</span><span style="color: #000000"&gt;,stu_name </span>= <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;张三</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;,stu_address </span>= <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;上海市</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;
        };
        </span><span style="color: #0000ff"&gt;String</span> id_str = <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;stu_id</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
        </span><span style="color: #0000ff"&gt;String</span> name_str = <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;stu_name</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
        </span><span style="color: #0000ff"&gt;String</span> addr_str = <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;stu_address</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
        Type type </span>= model_stu.GetType();<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;获取类型</span>
        PropertyInfo property_info_id =<span style="color: #000000"&gt; type.GetProperty(id_str);
        PropertyInfo property_info_name </span>=<span style="color: #000000"&gt; type.GetProperty(name_str);
        PropertyInfo property_info_addr </span>=<span style="color: #000000"&gt; type.GetProperty(addr_str);

        Console.WriteLine(property_info_id.GetValue(model_stu));
        Console.WriteLine(property_info_name.GetValue(model_stu));
        Console.WriteLine(property_info_addr.GetValue(model_stu));
        Console.ReadKey();
        </span><span style="color: #0000ff"&gt;#endregion</span><span style="color: #000000"&gt;

    }
}
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;class</span><span style="color: #000000"&gt; student
{
    </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;int</span> stu_id { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; }
    </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;String</span> stu_name { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; }
    </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;String</span> stu_address { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; }
}

}

title">三、Python

title">  1.截图

title="C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值" alt="C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值" src="https://cn.js-code.com/res/2019/02-06/17/a796355b32463e45f97f65dcd8173978.png" >  

title">2.源码

https://cn.js-code.com/res/2019/02-06/17/1c53668bcee393edac0d7b3b3daff1ae.gif" alt="">https://cn.js-code.com/res/2019/02-06/17/405b18b4b6584ae338e0f6ecaf736533.gif" alt="">
= jentZhang student: self.id = self.name = self.addr = stu = student(1,张三, v_id = v_name = v_addr = (hasattr(stu,v_id)) (hasattr(stu,)) ( (getattr(stu,v_id,5)) (getattr(stu,v_name,李四)) (getattr(stu,,李四)) ( setattr(stu,1000) setattr(stu,王五) setattr(stu,v_addr,) u.id) stu.@R_616_8313@ stu.addr) == main()

<p class="title">四、总结

  个人更喜欢Python的处理方式,非常灵活,大爱Python。

大佬总结

以上是大佬教程为你收集整理的C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值全部内容,希望文章能够帮你解决C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: