Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PropertyDescriptor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Reflection;
5 
6 namespace SiliconStudio.Core.Reflection
7 {
8  /// <summary>
9  /// A <see cref="IMemberDescriptor"/> for a <see cref="PropertyInfo"/>
10  /// </summary>
12  {
13  private readonly PropertyInfo propertyInfo;
14  private readonly MethodInfo getMethod;
15  private readonly MethodInfo setMethod;
16 
17  /// <summary>
18  /// Initializes a new instance of the <see cref="PropertyDescriptor" /> class.
19  /// </summary>
20  /// <param name="factory">The factory.</param>
21  /// <param name="propertyInfo">The property information.</param>
22  /// <exception cref="System.ArgumentNullException">propertyInfo</exception>
23  public PropertyDescriptor(ITypeDescriptorFactory factory, PropertyInfo propertyInfo)
24  : base(factory, propertyInfo)
25  {
26  if (propertyInfo == null) throw new ArgumentNullException("propertyInfo");
27 
28  this.propertyInfo = propertyInfo;
29 
30  getMethod = propertyInfo.GetGetMethod(false);
31  if (propertyInfo.CanWrite && propertyInfo.GetSetMethod(false) != null)
32  {
33  setMethod = propertyInfo.GetSetMethod(false);
34  }
35  this.TypeDescriptor = Factory.Find(propertyInfo.PropertyType);
36  }
37 
38  /// <summary>
39  /// Gets the property information attached to this instance.
40  /// </summary>
41  /// <value>The property information.</value>
42  public PropertyInfo PropertyInfo
43  {
44  get
45  {
46  return propertyInfo;
47  }
48  }
49 
50  public override Type Type
51  {
52  get
53  {
54  return propertyInfo.PropertyType;
55  }
56  }
57 
58  public override object Get(object thisObject)
59  {
60  return getMethod.Invoke(thisObject, null);
61  }
62 
63  public override void Set(object thisObject, object value)
64  {
65  if (HasSet)
66  setMethod.Invoke(thisObject, new[] {value});
67  }
68 
69  public override bool HasSet
70  {
71  get
72  {
73  return setMethod != null;
74  }
75  }
76 
77  /// <summary>
78  /// Returns a <see cref="System.String" /> that represents this instance.
79  /// </summary>
80  /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
81  public override string ToString()
82  {
83  return string.Format("Property [{0}] from Type [{1}]", Name, PropertyInfo.DeclaringType != null ? PropertyInfo.DeclaringType.FullName : string.Empty);
84  }
85  }
86 }
A IMemberDescriptor for a PropertyInfo
override object Get(object thisObject)
Gets the value of this member for the specified instance.
override void Set(object thisObject, object value)
Sets a value of this member for the specified instance.
Base class for IMemberDescriptor for a MemberInfo
A factory to create an instance of a ITypeDescriptor
override string ToString()
Returns a System.String that represents this instance.
PropertyDescriptor(ITypeDescriptorFactory factory, PropertyInfo propertyInfo)
Initializes a new instance of the PropertyDescriptor class.