How to acquire Result<dynamic>?
See original GitHub issueWhat is the current behavior?
When working with dynamic objects under CSharpFunctionalExtensions, dynamic is returned instead of Result<dynamic>.
What is the expected behavior?
When working with dynamic objects under CSharpFunctionalExtensions, Result<dynamic> should be returned, just as one can return List<dynamic>. Similarly, when mapping to type T, Result<T> should be returned.
Steps to reproduce
using System;
using System.Collections.Generic;
using CSharpFunctionalExtensions;
using FluentAssertions;
using Xunit;
namespace Locnes.Kernel.Test.CrossCutting.Extensions
{
public class ResultExtensionsLocalTest
{
[Fact]
public void Dynamic_result_can_be_cast_as_dynamic_result()
{
dynamic value = "Test";
var result = Result.Ok(value); // Expect Result<dynamic>, result is dynamic
var cast = (Result<dynamic>)result; // Fails
cast.Value.Should().Be(value);
}
[Fact]
public void Method_can_return_dynamic_result()
{
var result = GetResult(); // Fails
result.Value.Should().Be("Test");
}
[Fact]
public void Dynamic_result_can_be_cast_to_concrete_type()
{
dynamic value = "Test";
var result = Result.Ok(value)
.Map((Func<dynamic,string>)(v => (string)v)); // Result is still dynamic! Fails with error that Map does not exist
result.GetType().Should().Be(typeof(Result<string>));
}
[Fact]
public void String_result_provided_for_comparison()
{
var result = Result.Ok("Test").Map(v => (string) v);
result.GetType().Should().Be(typeof(Result<string>));
}
[Fact]
public void Method_can_return_dynamic_list()
{
var list = GetList();
list.Should().BeEmpty();
}
private Result<dynamic> GetResult() // Fails
{
dynamic value = "Test";
return Result.Ok(value);
}
private List<dynamic> GetList() // This works fine
{
return new List<dynamic>();
}
}
}
Additional info
I’m using version 1.19.1; I’ve not been able to make the migration to v2 yet, but I assume this has more to do with my understanding than CSharpFunctionalExtensions itself. I’ve found myself doing this because I need to manage different XML versions of effectively the same content; I’m attempting to do so using AmazedSaint.ElasticObject, as described here. Any suggestions how to return Result<dynamic> and enable mapping to concrete classes would be much appreciated. If the issue does happen to be version-specific, please let me know and I’ll take the plunge to upgrade to v2.
Simply phenomenal library you folks have created here, btw.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (4 by maintainers)

Top Related StackOverflow Question
@vkhorikov Sorry for the delay, but: it works! Thanks for the keen eye. 😃 I’ll go ahead and close this out.
@hankovich Yeah, it should. Pushed an update.