ln.types/reflection/TypeDescriptor.cs

46 lines
1.7 KiB
C#

// /**
// * File: TypeDescriptor.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.Collections.Generic;
using System.Reflection;
namespace ln.types.reflection
{
public class TypeDescriptor
{
public Type Type { get; }
Dictionary<string, AttributeDescriptor> attributeDescriptors = new Dictionary<string, AttributeDescriptor>();
public TypeDescriptor(Type type)
{
typeDescriptors[type] = this;
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
attributeDescriptors.Add(fieldInfo.Name, new AttributeDescriptor(fieldInfo));
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
attributeDescriptors.Add(propertyInfo.Name, new AttributeDescriptor(propertyInfo));
}
public IEnumerable<string> AttributeNames => attributeDescriptors.Keys;
public AttributeDescriptor GetAttributeDescriptor(string attributeName) => attributeDescriptors[attributeName];
public bool ContainsAttribute(string attributeName) => attributeDescriptors.ContainsKey(attributeName);
static Dictionary<Type, TypeDescriptor> typeDescriptors = new Dictionary<Type, TypeDescriptor>();
public static TypeDescriptor GetDescriptor(Type type)
{
if (typeDescriptors.TryGetValue(type, out TypeDescriptor typeDescriptor))
return new TypeDescriptor(type);
return typeDescriptor;
}
}
}