找传奇、传世资源到传世资源站!

c# 通过反射创建实例并设置属性的例子源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

通过Reflection创建实例

using System;using System.Reflection;public class Foo{ public string Bar { get; set; }}public class Program{ static void Main() { string name = "Foo"; string property = "Bar"; string value = "Baz"; // Get the type contained in the name string Type type = Type.GetType(name, true); // create an instance of that type object instance = Activator.CreateInstance(type); // Get a property on the type that is stored in the // property string PropertyInfo prop = type.GetProperty(property); // Set the value of the given property on the given instance prop.SetValue(instance, value, null); // at this stage instance.Bar will equal to the value Console.WriteLine(((Foo)instance).Bar); }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复