forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyDescriptor.cs
69 lines (55 loc) · 2.51 KB
/
PropertyDescriptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Stride.Core.Reflection
{
/// <summary>
/// A <see cref="IMemberDescriptor"/> for a <see cref="PropertyInfo"/>
/// </summary>
public class PropertyDescriptor : MemberDescriptorBase
{
private readonly MethodInfo getMethod;
private readonly MethodInfo setMethod;
public PropertyDescriptor(ITypeDescriptor typeDescriptor, PropertyInfo propertyInfo, StringComparer defaultNameComparer)
: base(propertyInfo, defaultNameComparer)
{
if (propertyInfo == null) throw new ArgumentNullException(nameof(propertyInfo));
PropertyInfo = propertyInfo;
getMethod = propertyInfo.GetMethod;
setMethod = propertyInfo.SetMethod;
TypeDescriptor = typeDescriptor;
}
/// <summary>
/// Gets the property information attached to this instance.
/// </summary>
/// <value>The property information.</value>
public PropertyInfo PropertyInfo { get; }
public override Type Type => PropertyInfo.PropertyType;
public sealed override bool IsPublic => getMethod?.IsPublic ?? false;
public override bool HasSet => setMethod != null;
public override object Get(object thisObject)
{
return getMethod.Invoke(thisObject, null);
}
public override void Set(object thisObject, object value)
{
if (!HasSet)
throw new InvalidOperationException($"The property [{Name}] of type [{DeclaringType.Name}] has no setter.");
setMethod.Invoke(thisObject, new[] {value});
}
public override IEnumerable<T> GetCustomAttributes<T>(bool inherit)
{
return PropertyInfo.GetCustomAttributes<T>(inherit);
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return $"Property [{Name}] from Type [{(PropertyInfo.DeclaringType != null ? PropertyInfo.DeclaringType.FullName : string.Empty)}]";
}
}
}