I created a T4 template to generate a partial class.
It references one of my DLLs:
<#@ assembly name="$(ProjectDir)..\Base\bin\Debug\net8.0\Base.dll" #>
If the DLL doesn't exist, my template gives an error. I want it to run without errors.
If the DLL exist, I want to generate a class and implement it's methods. If not, I want to generate the class with empty methods.
How to achieve this?
You'll need to use
System.Reflectionand runtime assembly loading - using<#@ assemblyconfigures a compile-time assembly reference for the implicit assembly generated from your T4 file, which obviously won't work if the assembly doesn't exist.I see you're using
$(ProjectDir)which can be an MSBuild parameter, or passed as an environment-variable, or accesed viathis.Hostin T4, or by being strongly-coupled to VS viaEnvDTE- so depending on how the T4 is being executed (T4 files normally run within Visual Studio, but they also need to run underdotnet buildormsbuidlwhereEnvDTEisn't available. You need to figure out that requirement and choose the appropriate response.But other than that, do something like this: