-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.cs
More file actions
70 lines (61 loc) · 2.53 KB
/
Copy pathGenerator.cs
File metadata and controls
70 lines (61 loc) · 2.53 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Reflection.Randomness
{
public class FromDistribution : Attribute
{
public FromDistribution(Type type)
{
if (type != typeof(NormalDistribution))
throw new ArgumentException($"Check declaration parameters of the type: {type.FullName}");
}
public FromDistribution(Type type, double lambda)
{
if (type != typeof(ExponentialDistribution))
throw new ArgumentException($"Check declaration parameters of the type: {type.FullName}");
}
public FromDistribution(Type type, double mean, double sigma)
{
if (type != typeof(NormalDistribution))
throw new ArgumentException($"Check declaration parameters of the type: {type.FullName}");
}
public FromDistribution(Type type, double mean, double sigma, double lambda)
{
throw new ArgumentException($"Check declaration parameters of the type: {type.FullName}");
}
}
public class Generator<TType> where TType : new()
{
private static PropertyInfo[] Properties { get; set; }
private readonly Dictionary<PropertyInfo, IContinuousDistribution> _propertiesWithDistributions =
new Dictionary<PropertyInfo, IContinuousDistribution>();
public Generator()
{
Properties = typeof(TType)
.GetProperties()
.Where(p => p.GetCustomAttributes(typeof(FromDistribution), false).Length != 0)
.ToArray();
}
public TType Generate(Random rnd)
{
var result = new TType();
foreach (var property in Properties)
{
if (_propertiesWithDistributions.ContainsKey(property))
{
property.SetValue(result, _propertiesWithDistributions[property].Generate(rnd));
continue;
}
var attributeArgs = property.CustomAttributes.First().ConstructorArguments.ToArray();
var values = attributeArgs.Skip(1).Select(a => a.Value).ToArray();
var distribution =
Activator.CreateInstance((Type) attributeArgs[0].Value, values) as IContinuousDistribution;
property.SetValue(result, distribution?.Generate(rnd));
_propertiesWithDistributions.Add(property, distribution);
}
return result;
}
}
}