kniemiec
kniemiec
CC#
Created by kniemiec on 3/29/2025 in #help
Handling Custom Assembly Attributes in Unit Tests After Migrating from .NET Framework 4.8 to .NET 8
3)
if (originSetupPackageAttributes == null ||
originSetupPackageAttributes.Length != 1 ||
!(originSetupPackageAttributes[0] is OriginSetupPackageAttribute))
{
throw new Exception("The program assembly has no OriginSetupPackage attribute.");
}
if (originSetupPackageAttributes == null ||
originSetupPackageAttributes.Length != 1 ||
!(originSetupPackageAttributes[0] is OriginSetupPackageAttribute))
{
throw new Exception("The program assembly has no OriginSetupPackage attribute.");
}
4)
[assembly: OriginSetupPackage(SetupPackage.Master)]

namespace App.Test
{
public class InitTest
{
public InitTest()
{
Assembly assembly = Assembly.GetCallingAssembly();

AppDomainManager manager = new();
FieldInfo entryAssemblyField = manager.GetType().GetField("_entryAssembly", BindingFlags.Instance | BindingFlags.NonPublic);
entryAssemblyField.SetValue(manager, assembly);

AppDomain domain = AppDomain.CurrentDomain;
FieldInfo domainManagerField = domain.GetType().GetField("_domainManager", BindingFlags.Instance | BindingFlags.NonPublic);
domainManagerField.SetValue(domain, manager);

Product.Initialize($"Server={TestConfig.ServerAddress}:{TestConfig.ServerPort}");
}
}
}
[assembly: OriginSetupPackage(SetupPackage.Master)]

namespace App.Test
{
public class InitTest
{
public InitTest()
{
Assembly assembly = Assembly.GetCallingAssembly();

AppDomainManager manager = new();
FieldInfo entryAssemblyField = manager.GetType().GetField("_entryAssembly", BindingFlags.Instance | BindingFlags.NonPublic);
entryAssemblyField.SetValue(manager, assembly);

AppDomain domain = AppDomain.CurrentDomain;
FieldInfo domainManagerField = domain.GetType().GetField("_domainManager", BindingFlags.Instance | BindingFlags.NonPublic);
domainManagerField.SetValue(domain, manager);

Product.Initialize($"Server={TestConfig.ServerAddress}:{TestConfig.ServerPort}");
}
}
}
7 replies
CC#
Created by kniemiec on 3/29/2025 in #help
Handling Custom Assembly Attributes in Unit Tests After Migrating from .NET Framework 4.8 to .NET 8
Specyfing
#if UNIT_TEST
#if UNIT_TEST
was also something I thought about, but I was wondering if it is valid solution. Also solution that doesn't involve modyfing Product class code would be the best, since it is not my code. But if it is the only workaround I'll probably need to convice someone to implement it that way Here is code, I had to make it screenshot due to character limitation 1)
[assembly: OriginSetupPackage(SetupPackage.Master)]

namespace App
{
// Some app code...
}
[assembly: OriginSetupPackage(SetupPackage.Master)]

namespace App
{
// Some app code...
}
2)
namespace App.Environment
{
public class Product
{
static Product()
{
// This method call needs to read the OriginSetupPackage attribute from the entry assembly
m_ProductPackageLazy = new Lazy<SetupPackage>(() =>
{
object[] originSetupPackageAttributes =
System.Reflection.Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(OriginSetupPackageAttribute), false);

if (originSetupPackageAttributes == null ||
originSetupPackageAttributes.Length != 1 ||
!(originSetupPackageAttributes[0] is OriginSetupPackageAttribute))
{
throw new Exception("The program assembly has no OriginSetupPackage attribute.");
}

return ((OriginSetupPackageAttribute)originSetupPackageAttributes[0]).Package;
});
}

private static Lazy<SetupPackage> m_ProductPackageLazy;
}
}
namespace App.Environment
{
public class Product
{
static Product()
{
// This method call needs to read the OriginSetupPackage attribute from the entry assembly
m_ProductPackageLazy = new Lazy<SetupPackage>(() =>
{
object[] originSetupPackageAttributes =
System.Reflection.Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(OriginSetupPackageAttribute), false);

if (originSetupPackageAttributes == null ||
originSetupPackageAttributes.Length != 1 ||
!(originSetupPackageAttributes[0] is OriginSetupPackageAttribute))
{
throw new Exception("The program assembly has no OriginSetupPackage attribute.");
}

return ((OriginSetupPackageAttribute)originSetupPackageAttributes[0]).Package;
});
}

private static Lazy<SetupPackage> m_ProductPackageLazy;
}
}
7 replies
CC#
Created by kniemiec on 3/29/2025 in #help
Handling Custom Assembly Attributes in Unit Tests After Migrating from .NET Framework 4.8 to .NET 8
1. It has to be entry assembly. But if it wasnt for it, how would you solve it? 2. Unfortunately, it has to. DI would be ideal but this is comming from some old code, which lets say, nowadays you wouldn't write. So yes, is has to be like this and in static constructor, is there a way to mock static constructor? Any solution or idea would be appreaciated
7 replies