-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathContextPropertyAttribute.cs
More file actions
67 lines (56 loc) · 2.39 KB
/
Copy pathContextPropertyAttribute.cs
File metadata and controls
67 lines (56 loc) · 2.39 KB
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
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Linq;
using OneScript.Commons;
namespace OneScript.Contexts
{
[AttributeUsage(AttributeTargets.Property)]
public class ContextPropertyAttribute : Attribute, INameAndAliasProvider
{
private readonly string _name;
private readonly string _alias;
private Type _converter;
public ContextPropertyAttribute(string name, string alias = "")
{
if (!Utils.IsValidIdentifier(name))
throw new ArgumentException("Name must be a valid identifier");
if (!string.IsNullOrEmpty(alias) && !Utils.IsValidIdentifier(alias))
throw new ArgumentException("Alias must be a valid identifier");
_name = name;
_alias = alias;
CanRead = true;
CanWrite = true;
}
public bool CanRead { get; set; }
public bool CanWrite { get; set; }
/// <summary>
/// Данное свойство не будет обработано генератором документации при обходе типов
/// </summary>
public bool SkipForDocumenter { get; set; }
public string Name => _name;
public string Alias => _alias;
/// <summary>
/// Конвертер значения свойства
/// </summary>
public Type Converter
{
get => _converter;
set
{
if (value != null)
{
if (!value.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IContextValueConverter<>)))
throw new Exception("Конвертер должен реализовывать интерфейс IContextValueConverter");
if (value.IsAbstract || value.IsInterface)
throw new Exception("Конвертер не может быть абстрактным типом или интерфейсом");
}
_converter = value;
}
}
}
}