C# 怎么使用get获取属性
时间: 2023-11-22 07:52:48 浏览: 36
在 C# 中,可以使用 `get` 访问器获取属性的值。以下是一个简单的示例代码:
```
public class Person
{
private string name;
public string Name
{
get
{
return name;
}
}
}
```
在上面的代码中,我们定义了一个 `Person` 类,并在其中创建了一个 `Name` 属性。`get` 访问器用于获取该属性的值。在 `get` 访问器中,我们只需返回要获取的属性值即可。
当我们在其他代码中访问 `Person` 类的 `Name` 属性时,将自动调用 `get` 访问器并返回该属性的值,如下所示:
```
Person person = new Person();
string name = person.Name; // 调用 get 访问器获取 Name 属性的值
```
相关问题
c# 对象 动态 获取属性值
在 C# 中,可以使用反射机制动态获取对象的属性值。以下是一个简单的示例代码:
```
using System;
using System.Reflection;
class Program {
static void Main() {
// 创建一个示例对象
MyObject obj = new MyObject();
// 获取属性值
PropertyInfo propInfo = obj.GetType().GetProperty("MyProperty");
object value = propInfo.GetValue(obj);
// 输出属性值
Console.WriteLine(value);
}
}
class MyObject {
public string MyProperty { get; set; } = "Hello, world!";
}
```
在上面的示例代码中,我们首先创建了一个 `MyObject` 类的示例对象 `obj`,然后使用 `GetType()` 方法获取对象的类型,再使用 `GetProperty()` 方法获取 `MyProperty` 属性的 `PropertyInfo` 对象,最后使用 `GetValue()` 方法获取属性值并将其存储在 `value` 变量中。
需要注意的是,反射机制会影响程序的性能,因此在实际开发中应该谨慎使用。
c#反射获取注解属性
C#中可以使用反射来获取带有注解的属性。注解属性可以通过Attribute类的派生类来定义和使用。下面是一个获取注解属性的示例:
假设有以下定义的注解属性:
```csharp
[AttributeUsage(AttributeTargets.Property)]
public class MyAttribute : Attribute
{
public string Name { get; set; }
public MyAttribute(string name)
{
Name = name;
}
}
```
有以下定义的类:
```csharp
public class MyClass
{
[MyAttribute("属性1")]
public string Property1 { get; set; }
[MyAttribute("属性2")]
public string Property2 { get; set; }
}
```
可以使用反射来获取注解属性信息:
```csharp
MyClass myClass = new MyClass();
Type type = myClass.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
MyAttribute myAttribute = (MyAttribute)Attribute.GetCustomAttribute(property, typeof(MyAttribute));
if (myAttribute != null)
{
Console.WriteLine($"属性名称:{property.Name},注解名称:{myAttribute.Name}");
}
}
```
在上面的示例中,我们使用了Type.GetProperties()方法来获取MyClass类的属性信息,然后使用Attribute.GetCustomAttribute()方法来获取带有MyAttribute注解的属性信息,并输出了属性名称和注解名称。
需要注意的是,Attribute.GetCustomAttribute()方法返回的是一个Attribute类型的对象,需要进行强制类型转换才能获取到注解属性的具体信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)