-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSourceTypeInfoExtensions.cs
23 lines (18 loc) · 1.19 KB
/
SourceTypeInfoExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace SourceGeneration.Reflection;
public static class SourceTypeInfoExtensions
{
public static ISourceFieldOrPropertyInfo[] GetFieldsAndProperties(this SourceTypeInfo type) => type.GetFields().Cast<ISourceFieldOrPropertyInfo>().Union(type.GetProperties()).ToArray();
public static ISourceFieldOrPropertyInfo? GetFieldOrProperty(this SourceTypeInfo type, string name) => (ISourceFieldOrPropertyInfo?)type.GetField(name) ?? type.GetProperty(name);
public static ISourceFieldOrPropertyInfo GetRequriedFieldOrProperty(this SourceTypeInfo type, string name)
{
return (ISourceFieldOrPropertyInfo?)type.GetField(name) ?? type.GetProperty(name) ?? throw new KeyNotFoundException($"Field or property '{name}' not found.");
}
public static SourceFieldInfo GetRequriedField(this SourceTypeInfo sourceTypeInfo, string name)
{
return sourceTypeInfo.GetField(name) ?? throw new KeyNotFoundException($"Field '{name}' not found.");
}
public static SourcePropertyInfo GetRequriedProperty(this SourceTypeInfo sourceTypeInfo, string name)
{
return sourceTypeInfo.GetProperty(name) ?? throw new KeyNotFoundException($"Property '{name}' not found.");
}
}