Try the following code:
class Program
{
static void Main(string[] args)
{
Type type = typeof(A);
Type type2 = typeof(B);
var c1 = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(t=>t.PropertyType == type2);
var c2 = type2.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(t=>t.PropertyType == type);
if (c1.Count()!=0 && c2.Count()!=0)
{
Console.WriteLine("They are circularly dependent");
}
}
}
public class A
{
public B TypeB { get; set; }
}
public class B
{
public A TypeA { get; set; }
}
Did I understand what you mean correctly?
Update:
You said that they are the relationship between the parent type and the child type, but the above example does not seem to reflect this relationship.
If they have an inheritance relationship, then even if we don't know the exact type of the subclass, we should be able to put it in the collection of the parent class, and then use reflection to get the exact type and get all the properties.
class Program
{
static void Main()
{
List<A> classes = GetA();
foreach (var item in classes)
{
Console.WriteLine(item.GetType());
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
public static List<A> GetA()
{
List<A> classes = new List<A>();
classes.Add(new A());
classes.Add(new B());
classes.Add(new C());
return classes;
}
}
class A
{
public B TypeB { get; set; }
}
class B:A
{
public C TypeC { get; set; }
}
class C:B
{
public A TypeA { get; set; }
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.