❔ Get property from type

Through reflection I get all the types matching the interface.
public void GetRegularTypes()
{
var interfaceType = typeof(IRegularPuzzle);
var implementingTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(type => interfaceType.IsAssignableFrom(type)
&& !type.IsAbstract
&& !type.IsInterface);

foreach (var type in implementingTypes)
{
PropertyInfo nameProperty = type.GetProperty("Name");
string name = (string)nameProperty.GetValue(null);
_regularTypes[name.ToLower()] = type;
}
}
public void GetRegularTypes()
{
var interfaceType = typeof(IRegularPuzzle);
var implementingTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(type => interfaceType.IsAssignableFrom(type)
&& !type.IsAbstract
&& !type.IsInterface);

foreach (var type in implementingTypes)
{
PropertyInfo nameProperty = type.GetProperty("Name");
string name = (string)nameProperty.GetValue(null);
_regularTypes[name.ToLower()] = type;
}
}
However, a class has for example:
public class NineByNineRegularPuzzle : IRegularPuzzle
{
public string Name => "9x9";

public int Size => 9;
}
public class NineByNineRegularPuzzle : IRegularPuzzle
{
public string Name => "9x9";

public int Size => 9;
}
Is there a way I can get the Name property in the foreach? What i tried didnt work.
10 Replies
electronic heartbreak.
Or is it OK to rename the file to 6x6RegularPuzzle?
ero
ero2y ago
why are you using reflection?
electronic heartbreak.
Since i dynamically need the puzzles. I first had a list with the extensions but that was not good. This works:
public void GetRegularTypes()
{
var interfaceType = typeof(IRegularPuzzle);
var implementingTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(type => interfaceType.IsAssignableFrom(type)
&& !type.IsAbstract
&& !type.IsInterface);

foreach (var type in implementingTypes)
{
IRegularPuzzle puzzle = (IRegularPuzzle)Activator.CreateInstance(type);
_regularTypes[puzzle.Name] = puzzle;
}
}
public void GetRegularTypes()
{
var interfaceType = typeof(IRegularPuzzle);
var implementingTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(type => interfaceType.IsAssignableFrom(type)
&& !type.IsAbstract
&& !type.IsInterface);

foreach (var type in implementingTypes)
{
IRegularPuzzle puzzle = (IRegularPuzzle)Activator.CreateInstance(type);
_regularTypes[puzzle.Name] = puzzle;
}
}
but i activate all the instances pain
DarkVader
DarkVader2y ago
Try this var nameProperty = type .GetProperties() .First(x => x.Name == "Name"); You can get the value from that easily Sorry for the formatting I'm on my phone
Pascal
Pascal2y ago
Type names can not begin with numbers
electronic heartbreak.
I got static issues when doing that
DarkVader
DarkVader2y ago
Why can't you make the method static?
electronic heartbreak.
The method? Sorry it's 8 am and I just woke up
DarkVader
DarkVader2y ago
GetRegularTypes function or method, whatever you like to call it. Good morning then
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server