Tanoda
ClassTypeConstraintAttribute.cs
Go to the documentation of this file.
1// Copyright (c) Rotorz Limited. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root.
3
4using System;
5using UnityEngine;
6
7namespace TypeReferences {
8
12 public enum ClassGrouping {
16 None,
31 }
32
37 public abstract class ClassTypeConstraintAttribute : PropertyAttribute {
38
39 private ClassGrouping _grouping = ClassGrouping.ByNamespaceFlat;
40 private bool _allowAbstract = false;
41
47 get { return _grouping; }
48 set { _grouping = value; }
49 }
50
55 public bool AllowAbstract {
56 get { return _allowAbstract; }
57 set { _allowAbstract = value; }
58 }
59
68 public virtual bool IsConstraintSatisfied(Type type) {
69 return AllowAbstract || !type.IsAbstract;
70 }
71
72 }
73
78 [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
80
85 }
86
91 public ClassExtendsAttribute(Type baseType) {
92 BaseType = baseType;
93 }
94
98 public Type BaseType { get; private set; }
99
101 public override bool IsConstraintSatisfied(Type type) {
102 return base.IsConstraintSatisfied(type)
103 && BaseType.IsAssignableFrom(type) && type != BaseType;
104 }
105
106 }
107
112 [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
114
119 }
120
125 public ClassImplementsAttribute(Type interfaceType) {
126 InterfaceType = interfaceType;
127 }
128
132 public Type InterfaceType { get; private set; }
133
135 public override bool IsConstraintSatisfied(Type type) {
136 if (base.IsConstraintSatisfied(type)) {
137 foreach (var interfaceType in type.GetInterfaces())
138 if (interfaceType == InterfaceType)
139 return true;
140 }
141 return false;
142 }
143
144 }
145
146}
Constraint that allows selection of classes that extend a specific class when selecting a ClassTypeRe...
ClassExtendsAttribute()
Initializes a new instance of the ClassExtendsAttribute class.
override bool IsConstraintSatisfied(Type type)
Determines whether the specified Type satisfies filter constraint.
ClassExtendsAttribute(Type baseType)
Initializes a new instance of the ClassExtendsAttribute class.
Type BaseType
Gets the type of class that selectable classes must derive from.
Constraint that allows selection of classes that implement a specific interface when selecting a Clas...
Type InterfaceType
Gets the type of interface that selectable classes must implement.
ClassImplementsAttribute(Type interfaceType)
Initializes a new instance of the ClassImplementsAttribute class.
ClassImplementsAttribute()
Initializes a new instance of the ClassImplementsAttribute class.
override bool IsConstraintSatisfied(Type type)
Determines whether the specified Type satisfies filter constraint.
Base class for class selection constraints that can be applied when selecting a ClassTypeReference wi...
bool AllowAbstract
Gets or sets whether abstract classes can be selected from drop-down. Defaults to a value of false un...
ClassGrouping Grouping
Gets or sets grouping of selectable classes. Defaults to ClassGrouping.ByNamespaceFlat unless explici...
virtual bool IsConstraintSatisfied(Type type)
Determines whether the specified Type satisfies filter constraint.
ClassGrouping
Indicates how selectable classes should be collated in drop-down menu.
@ ByNamespace
Group classes by namespace and show foldout menus for nested namespaces; for instance,...
@ ByNamespaceFlat
Group classes by namespace; for instance, "Some.Nested.Namespace > SpecialClass".
@ ByAddComponentMenu
Group classes in the same way as Unity does for its component menu. This grouping method must only be...