Excel-DNA / ExcelDna

Excel-DNA - Free and easy .NET for Excel. This repository contains the core Excel-DNA library.
https://excel-dna.net
zlib License
1.29k stars 274 forks source link

Extended registration - consider supporting async with object handles #717

Open govert opened 2 weeks ago

govert commented 2 weeks ago

With the extended registration planned to Excel-DNA 1.9, we support both Task-based async methods and for 'unknown' data types we support object handles. But currently these can't be mixed. We should consider whether to extend the type conversions into the async methods

For example, all the Async functions here fail with #VALUE

using System.Threading.Tasks;
using ExcelDna.Integration;

namespace TestAsyncObject190
{
    public class MyData
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }

    public class Functions
    {
        [ExcelFunction]
        public static MyData GetData(string name)
        {
            var result = new MyData { Name = name, Value = name.Length };
            return result;
        }

        [ExcelFunction]
        public static string GetName(MyData data)
        {
            return data.Name;
        }

        [ExcelFunction]
        public static int GetValue(MyData data)
        {
            return data.Value;
        }

        [ExcelFunction]
        public static async Task<MyData> GetDataAsync(string name)
        {
            await Task.Delay(1);
            var result = new MyData { Name = name, Value = name.Length };
            return result;
        }

        [ExcelFunction]
        public static async Task<string> GetNameAsync(MyData data)
        {
            await Task.Delay(1);
            return data.Name;
        }

        [ExcelFunction]
        public static async Task<int> GetValueAsync(MyData data)
        {
            await Task.Delay(1);
            return data.Value;
        }

    }
}
govert commented 6 days ago

I suppose it would make sense for the custom conversion of data types to work inside an async Task<T> function the same as for a normal sync function.