TNG / ArchUnitNET

A C# architecture test library to specify and assert architecture rules in C# for automated testing.
Apache License 2.0
915 stars 60 forks source link

Q: How do I create an instance of IType from System.Type? #279

Open mmisztal1980 opened 3 months ago

mmisztal1980 commented 3 months ago

Using a custom ICondition<Class> I'm writing a set of tests to validate a set of constructors in my library's layer. I'm enumerating constructor's IType parameters, and I'd like to compare them vs expected System.Type(s) to make my test assertions. The IType has an .IsAssignableTo(IType) method, which I'd gladly use, however I'm not able to convert my System.Type(s) to an IType.

I'm it that to be somewhat difficult to do, and I haven't found anything in the docs yet. Can anybody explain how can this be achieved?

CesarD commented 3 months ago

I'm asking myself the same question. Any tip to share, @alexanderlinne?

CesarD commented 3 months ago

Ok, I found by some other people's example that you can do Architecture.GetClassOfType(typeof(YourClass)) or Architecture.GetInterfaceOfType(typeof(IYourInterface)) to get the instance that satisfies an IType interface for the lib methods.

I'd like to know if there's any better alternative, but this works.

Hope it helps.