I have the below extension method where I pass the type and cast the object to the type but get a compile error at casting.
public static void AddCellProperties(this ColumnInfo _columnInfo, Type type)
{
_columnInfo.SetCellUsing((c, o) =>
{
if (o == null) c.SetCellValue("NULL");
else c.SetCellValue((type)o); // getting an error here near "type"
})
.SetPropertyUsing(a => a as string == "NULL" ? null : Convert.ChangeType(a, type, CultureInfo.InvariantCulture));
}
But whereas this works.
public static void AddCellProperties(this ColumnInfo _columnInfo)
{
_columnInfo.SetCellUsing((c, o) =>
{
if (o == null) c.SetCellValue("NULL");
else c.SetCellValue((int)o);
})
.SetPropertyUsing(a => a as string == "NULL" ? null : Convert.ChangeType(a, typeof(int), CultureInfo.InvariantCulture));
}
I am just trying to replace the int
with type
so that caller can pass whatever type.
Could anyone please let me know how I can overcome this issue? Thanks in advance!!!