您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

2026-04-12 13:133阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计584个文字,预计阅读时间需要3分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

原文:本章节实例讲述了C语言。

本节以实例展示C语言知识。

本文实例讲述了C#通过属性名字符串获取、设置对象属性值操作.分享给大家供大家参考,具体如下:

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

#通过反射获取对象属性值并设置属性值

0、定义一个类

public class User { public int Id { get; set; } public string Name { get; set; } public string Age { get; set; } }

1、通过属性名(字符串)获取对象属性值

User u = new User(); u.Name = "lily"; var propName = "Name"; var propNameVal = u.GetType().GetProperty(propName).GetValue(u, null); Console.WriteLine(propNameVal);// "lily"

2、通过属性名(字符串)设置对象属性值

User u = new User(); u.Name = "lily"; var propName = "Name"; var newVal = "MeiMei"; u.GetType().GetProperty(propName).SetValue(u, newVal); Console.WriteLine(propNameVal);// "MeiMei"

#获取对象的所有属性名称及类型

通过类的对象实现

User u = new User(); foreach (var item in u.GetType().GetProperties()) { Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}"); } // propName: Id,propType: Int32 // propName:Name,propType: String // propName:Age,propType: String

通过类实现

foreach (var item in typeof(User).GetProperties()) { Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}"); } // propName: Id,propType: Int32 // propName:Name,propType: String // propName:Age,propType: String

#判断对象是否包含某个属性

static void Main(string[] args) { User u = new User(); bool isContain= ContainProperty(u,"Name");// true } public static bool ContainProperty( object instance, string propertyName) { if (instance != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName); return (_findedPropertyInfo != null); } return false; }

将其封装为扩展方法

public static class ExtendLibrary { /// <summary> /// 利用反射来判断对象是否包含某个属性 /// </summary> /// <param name="instance">object</param> /// <param name="propertyName">需要判断的属性</param> /// <returns>是否包含</returns> public static bool ContainProperty(this object instance, string propertyName) { if (instance != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName); return (_findedPropertyInfo != null); } return false; } } static void Main(string[] args) { User u = new User(); bool isContain= u.ContainProperty("Name");// true }

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#数据结构与算法教程》、《C#遍历算法与技巧总结》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》

希望本文所述对大家C#程序设计有所帮助。

本文共计584个文字,预计阅读时间需要3分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

原文:本章节实例讲述了C语言。

本节以实例展示C语言知识。

本文实例讲述了C#通过属性名字符串获取、设置对象属性值操作.分享给大家供大家参考,具体如下:

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

#通过反射获取对象属性值并设置属性值

0、定义一个类

public class User { public int Id { get; set; } public string Name { get; set; } public string Age { get; set; } }

1、通过属性名(字符串)获取对象属性值

User u = new User(); u.Name = "lily"; var propName = "Name"; var propNameVal = u.GetType().GetProperty(propName).GetValue(u, null); Console.WriteLine(propNameVal);// "lily"

2、通过属性名(字符串)设置对象属性值

User u = new User(); u.Name = "lily"; var propName = "Name"; var newVal = "MeiMei"; u.GetType().GetProperty(propName).SetValue(u, newVal); Console.WriteLine(propNameVal);// "MeiMei"

#获取对象的所有属性名称及类型

通过类的对象实现

User u = new User(); foreach (var item in u.GetType().GetProperties()) { Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}"); } // propName: Id,propType: Int32 // propName:Name,propType: String // propName:Age,propType: String

通过类实现

foreach (var item in typeof(User).GetProperties()) { Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}"); } // propName: Id,propType: Int32 // propName:Name,propType: String // propName:Age,propType: String

#判断对象是否包含某个属性

static void Main(string[] args) { User u = new User(); bool isContain= ContainProperty(u,"Name");// true } public static bool ContainProperty( object instance, string propertyName) { if (instance != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName); return (_findedPropertyInfo != null); } return false; }

将其封装为扩展方法

public static class ExtendLibrary { /// <summary> /// 利用反射来判断对象是否包含某个属性 /// </summary> /// <param name="instance">object</param> /// <param name="propertyName">需要判断的属性</param> /// <returns>是否包含</returns> public static bool ContainProperty(this object instance, string propertyName) { if (instance != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName); return (_findedPropertyInfo != null); } return false; } } static void Main(string[] args) { User u = new User(); bool isContain= u.ContainProperty("Name");// true }

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#数据结构与算法教程》、《C#遍历算法与技巧总结》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》

希望本文所述对大家C#程序设计有所帮助。