question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to acquire Result<dynamic>?

See original GitHub issue

What 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:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kriewallcommented, Dec 16, 2020

@vkhorikov Sorry for the delay, but: it works! Thanks for the keen eye. 😃 I’ll go ahead and close this out.

1reaction
vkhorikovcommented, Dec 2, 2020

@hankovich Yeah, it should. Pushed an update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Get Result View or Dynamic View of an object
You can use the dynamic variable and JObject.Parse to get values like this: dynamic my_object = JObject.Parse(json_string); Console.
Read more >
Query Fields Dynamically and Retrieve Results per Field
I'm trying to query a record's fields based on a calculated result from another record. So, if the result is "A", then on...
Read more >
How get a dynamic value and paste it.
My goal is to get a dynamic value and paste it. ... GetAttributeValueText("Text"); //Get the text value; result = result.Substring(result.
Read more >
How to create Dynamic Results Pages
You make your results page dynamic by creating content blocks and applying conditional logic to those blocks in order to show or hide...
Read more >
Get started with dynamic outputs
Create a sample action that builds dynamic objects for use in a flow. ... Topics are ranked in search results by how closely...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found