Skip to content

Commit b6fc1ac

Browse files
Remove a call to Type.GetProperty to improve AOT support (#2048)
EPPlus is mostly usable in a native AOT compilation context. One exception to this support is the use of the reflection method Type.GetProperty in TypeCompat.GetPropertyValue. Fortunately, there is only one place where this method is used, in ExcelVBACollectionBase. By adding a type constraint on the generic type T of ExcelVBACollectionBase, we can get rid of the use of reflection. In practice, an AOT compiled project that uses EPPlus to read an Excel file that contains VBA macro could throw NullReferenceExceptions.
1 parent 678e19e commit b6fc1ac

6 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/EPPlus/Compatibility/TypeCompat.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,5 @@ internal static bool IsGenericType(Type t)
4545
#endif
4646

4747
}
48-
public static object GetPropertyValue(object v, string name)
49-
{
50-
#if (Core)
51-
return v.GetType().GetTypeInfo().GetProperty(name).GetValue(v, null);
52-
#else
53-
return v.GetType().GetProperty(name).GetValue(v, null);
54-
#endif
55-
}
5648
}
5749
}

src/EPPlus/Vba/ExcelVBACollectionBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace OfficeOpenXml.VBA
2020
/// Base class for VBA collections
2121
/// </summary>
2222
/// <typeparam name="T"></typeparam>
23-
public class ExcelVBACollectionBase<T> : IEnumerable<T>
23+
public class ExcelVBACollectionBase<T> : IEnumerable<T> where T : IExcelVBACollectionElement
2424
{
2525
/// <summary>
2626
/// A list of vba objects
@@ -48,7 +48,7 @@ public T this [string Name]
4848
{
4949
get
5050
{
51-
return _list.Find((f) => TypeCompat.GetPropertyValue(f,"Name").ToString().Equals(Name,StringComparison.OrdinalIgnoreCase));
51+
return _list.Find((f) => f.Name.ToString().Equals(Name,StringComparison.OrdinalIgnoreCase));
5252
}
5353
}
5454
/// <summary>
@@ -77,7 +77,7 @@ public int Count
7777
/// <returns>True if the name exists</returns>
7878
public bool Exists(string Name)
7979
{
80-
return _list.Exists((f) => TypeCompat.GetPropertyValue(f,"Name").ToString().Equals(Name,StringComparison.OrdinalIgnoreCase));
80+
return _list.Exists((f) => f.Name.ToString().Equals(Name,StringComparison.OrdinalIgnoreCase));
8181
}
8282
/// <summary>
8383
/// Removes the item

src/EPPlus/Vba/ExcelVBAReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace OfficeOpenXml.VBA
1515
/// <summary>
1616
/// A VBA reference
1717
/// </summary>
18-
public class ExcelVbaReference
18+
public class ExcelVbaReference : IExcelVBACollectionElement
1919
{
2020
/// <summary>
2121
/// Constructor.

src/EPPlus/Vba/ExcelVbaModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace OfficeOpenXml.VBA
2121
/// <summary>
2222
/// A VBA code module.
2323
/// </summary>
24-
public class ExcelVBAModule
24+
public class ExcelVBAModule : IExcelVBACollectionElement
2525
{
2626
string _name = "";
2727
ModuleNameChange _nameChangeCallback = null;

src/EPPlus/Vba/ExcelVbaModuleAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace OfficeOpenXml.VBA
1515
/// <summary>
1616
/// A VBA modual attribute
1717
/// </summary>
18-
public class ExcelVbaModuleAttribute
18+
public class ExcelVbaModuleAttribute : IExcelVBACollectionElement
1919
{
2020
internal ExcelVbaModuleAttribute()
2121
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace OfficeOpenXml.VBA {
2+
3+
/// <summary>
4+
/// The interface that must be implemented by the elements stored by ExcelVBACollectionBase.
5+
/// </summary>
6+
public interface IExcelVBACollectionElement {
7+
string Name{get;}
8+
}
9+
}

0 commit comments

Comments
 (0)