62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
// /**
|
|
// * File: AttributeDescriptor.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using System.Reflection;
|
|
namespace ln.types.reflection
|
|
{
|
|
public class AttributeDescriptor
|
|
{
|
|
public bool IsPublic { get; }
|
|
public bool IsReadonly { get; }
|
|
|
|
public Type Owner { get; }
|
|
public string AttributeName { get; }
|
|
public Type AttributeType { get; }
|
|
|
|
Action<object, object> SetAction;
|
|
Func<object, object> GetFunc;
|
|
|
|
public AttributeDescriptor(PropertyInfo propertyInfo)
|
|
{
|
|
Owner = propertyInfo.DeclaringType;
|
|
IsPublic = propertyInfo.GetGetMethod().IsPublic;
|
|
IsReadonly = !(propertyInfo.CanWrite && propertyInfo.GetSetMethod(true).IsPublic);
|
|
AttributeName = propertyInfo.Name;
|
|
AttributeType = propertyInfo.PropertyType;
|
|
|
|
if (propertyInfo.CanWrite)
|
|
SetAction = propertyInfo.SetValue;
|
|
|
|
GetFunc = propertyInfo.GetValue;
|
|
}
|
|
public AttributeDescriptor(FieldInfo fieldInfo)
|
|
{
|
|
Owner = fieldInfo.DeclaringType;
|
|
IsPublic = fieldInfo.IsPublic;
|
|
IsReadonly = fieldInfo.IsInitOnly;
|
|
AttributeName = fieldInfo.Name;
|
|
AttributeType = fieldInfo.FieldType;
|
|
|
|
if (!fieldInfo.IsInitOnly)
|
|
SetAction = fieldInfo.SetValue;
|
|
|
|
GetFunc = fieldInfo.GetValue;
|
|
}
|
|
|
|
public TypeDescriptor TypeDescriptor => TypeDescriptor.GetDescriptor(Owner);
|
|
|
|
public object GetValue(object instance) => GetFunc(instance);
|
|
public void SetValue(object instance, object value) {
|
|
if (SetAction != null)
|
|
SetAction(instance, value);
|
|
}
|
|
}
|
|
}
|