-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureSystem.cs
More file actions
71 lines (57 loc) · 1.66 KB
/
Copy pathSecureSystem.cs
File metadata and controls
71 lines (57 loc) · 1.66 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
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _7._Attributes
{
class SecureSystem
{
private const int RequiredAccessLevel = 1;
public static void CheckAccessLevel(Employee employee)
{
Type type = employee.GetType();
object[] attributes = type.GetCustomAttributes(false);
foreach (var attribute in attributes)
{
if (attribute is AccessLevelAttribute)
{
AccessLevelAttribute accessLevelAttribute = (AccessLevelAttribute)attribute;
Console.WriteLine($"{type.Name} має рівень доступу {accessLevelAttribute.Level}");
if (accessLevelAttribute.Level > RequiredAccessLevel)
{
Console.WriteLine($"Доступ заборонено для {type.Name}");
}
else
{
Console.WriteLine($"Доступ дозволено для {type.Name}");
}
}
}
}
}
abstract class Employee
{
}
[AccessLevel(2)]
class Manager : Employee
{
}
[AccessLevel(1)]
class Director : Employee
{
}
[AccessLevel(1)]
class Programer : Employee
{
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
class AccessLevelAttribute : Attribute
{
public int Level { get; }
public AccessLevelAttribute(int level)
{
Level = level;
}
}
}