Actually you need to get TypeInfo using GetMembers method, by checking member type as nested type.
public static bool FindProperty(string className, IEnumerable<long> list)
{
var allSubClass = typeof(MainClass)
.GetMembers()
.Where(m => m.MemberType == System.Reflection.MemberTypes.NestedType);
var classLookingFor = allSubClass.FirstOrDefault(c => string.Equals(c.Name, className, System.StringComparison.InvariantCultureIgnoreCase));
if (classLookingFor == null)
{
Console.WriteLine("Class {0} not found.", className);
return false;
}
else
{
// get all properties, in fact they are fields type of 'long/Int64'
var allProp = (classLookingFor as TypeInfo).DeclaredFields.Where(f => f.FieldType == typeof(long));
var anyPropWithValue = allProp.Any(p => list.Any(lng => lng == (long)p.GetValue(null)));
return anyPropWithValue;
}
}
you can find the fiddle here. https://dotnetfiddle.net/d3dbYr
I guess its expected result -
var list = new List<long> { 36351526, 365635, 280847 };
MainClass.FindProperty("SubClass", list); // it returns 'true', as SubClass contains 365635
MainClass.FindProperty("SubClass2", list); // it returns 'true', as SubClass2 contains 36351526
MainClass.FindProperty("SubClass3", list); // it returns 'false' as there is not such class
list = new List<long> { 99999999 };
MainClass.FindProperty("SubClass", list); // it returns 'false' Sub class doesn't contain any property that has value as '99999999'
Update
you can modify the code little more on need -
var members = typeof(MainClass).GetMember(className);
if (!members.Any())
{
Console.WriteLine($"Class {className} not found.");
return false;
}
else
{
var allProp = (members[0] as TypeInfo).DeclaredFields.Where(f => f.FieldType == typeof(long));
var anyPropWithValue = allProp.Any(p => list.Any(lng => lng == (long)p.GetValue(null)));
return anyPropWithValue;
}