© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
12 replies
electronic heartbreak.

❔ 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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Get property from generic type?
C#CC# / help
4y ago
Retrive TypeDeclarationSyntax from a referenced type
C#CC# / help
4y ago
cannot convert from 'TypeA' to 'TypeB' with generics
C#CC# / help
2y ago
Error: Inconsistent accessibility: property type 'type' is less accessible than property 'property'
C#CC# / help
3y ago