ArchUnitNET icon indicating copy to clipboard operation
ArchUnitNET copied to clipboard

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

Open mmisztal1980 opened this issue 1 year ago • 2 comments

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?

mmisztal1980 avatar Aug 01 '24 18:08 mmisztal1980

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

CesarD avatar Aug 13 '24 09:08 CesarD

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.

CesarD avatar Aug 13 '24 11:08 CesarD

Hi @mmisztal1980, @CesarD already gave the correct answer. There's also the more generic method Architecture.GetITypeOfType(typeof(YourType)), which is probably best suited for your case. Feel free to reopen if there are any further questions.

alexanderlinne avatar Apr 25 '25 10:04 alexanderlinne