Skip to content

Commit

Permalink
provides an AOT-compatible way to load Agile from OOXML
Browse files Browse the repository at this point in the history
  • Loading branch information
karakasa committed Aug 13, 2023
1 parent 23574ac commit 36365be
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions main/POIFS/Crypt/EncryptionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ EncryptionMode encryptionMode
// REMOVE-REFLECTION: Remove reflections here will prevent NPOI from creating custom encryptors.
// Is it OK? Remove reflection-related code for now.
// It might be better to move Agile classes definition into this library. Now it's defined in NPOI.OOXML.Core.
#if NET6_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.Interfaces
| DynamicallyAccessedMemberTypes.PublicMethods, EncryptionMode.BuilderNameAgile, "NPOI.OOXML")]
#endif
protected static IEncryptionInfoBuilder GetBuilder(EncryptionMode encryptionMode)
{
switch (encryptionMode.Builder)
Expand All @@ -307,8 +311,6 @@ protected static IEncryptionInfoBuilder GetBuilder(EncryptionMode encryptionMode
case EncryptionMode.BuilderNameStandard:
return new Standard.StandardEncryptionInfoBuilder();
case EncryptionMode.BuilderNameAgile:
// return new Agile.AgileEncryptionInfoBuilder();
// TODO
IEncryptionInfoBuilder instance = CreateAgileInstanceFallback();
if (instance is not null)
return instance;
Expand All @@ -318,24 +320,20 @@ protected static IEncryptionInfoBuilder GetBuilder(EncryptionMode encryptionMode
throw new EncryptedDocumentException("Not found type " + encryptionMode.Builder);
}

private static Type AgileEncrpytionInfo;

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Agile must be dynamically loaded from NPOI.OOXML. TODO.")]
#endif
private static Type AgileEncryptionInfo;
private static IEncryptionInfoBuilder CreateAgileInstanceFallback()
{
if (AgileEncrpytionInfo is null)
if (AgileEncryptionInfo is null)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
if ((AgileEncrpytionInfo = assembly.GetType(EncryptionMode.BuilderNameAgile)) != null)
if ((AgileEncryptionInfo = assembly.GetType(EncryptionMode.BuilderNameAgile)) != null)
break;
}

if (AgileEncrpytionInfo is null)
if (AgileEncryptionInfo is null)
return null;

return (IEncryptionInfoBuilder)Activator.CreateInstance(AgileEncrpytionInfo);
return (IEncryptionInfoBuilder)Activator.CreateInstance(AgileEncryptionInfo);
}

private int _versionMajor;
Expand Down

0 comments on commit 36365be

Please sign in to comment.