-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
SubclassTypeValidator.cs
38 lines (31 loc) · 1.19 KB
/
SubclassTypeValidator.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Configuration
{
// This class validates that the value is a subclass of a specified type
public sealed class SubclassTypeValidator : ConfigurationValidatorBase
{
private readonly Type _base;
public SubclassTypeValidator(Type baseClass)
{
if (baseClass == null)
throw new ArgumentNullException(nameof(baseClass));
_base = baseClass;
}
public override bool CanValidate(Type type)
{
return type == typeof(Type);
}
public override void Validate(object value)
{
if (value == null) return;
// Make a check here since value.GetType() returns RuntimeType rather then Type
if (!(value is Type)) ValidatorUtils.HelperParamValidation(value, typeof(Type));
if (!_base.IsAssignableFrom((Type)value))
{
throw new ArgumentException(SR.Format(SR.Subclass_validator_error, ((Type)value).FullName,
_base.FullName));
}
}
}
}