inz-00/Assets/Scripts/Common/Metadata.cs

34 lines
1.0 KiB
C#

using System;
using System.Collections.Generic;
using Assets.Exceptions;
namespace Assets.Common
{
[Serializable]
public class Metadata
{
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
public bool HasProperty(string name) => _properties.ContainsKey(name);
public bool HasProperty<T>(string name) => HasProperty(name) && _properties[name] is T;
public void SetProperty<T>(string name, T property)
{
_properties[name] = property;
}
public T GetProperty<T>(string name)
{
if (!HasProperty(name))
throw new PropertyAccessException($"Property named \"{name}\" does not exist in this site.");
var property = _properties[name];
if (property is T value)
return value;
throw new PropertyAccessException($"Property named \"{name}\" is of type {property.GetType()} not {typeof(T)}.");
}
}
}