0

I am trying to understand how converting strings to objects works in C#. So I have the following string:

public async Task<String> FunctionReturningReponse() {
     var response = "{'ReponseText': 'Some Text'}";
     return response
}

Now I created a schema as following:

public class TriggerResponse
{
    public string? ReponseText { get; set; } = string.Empty;
}

Can I somehow use this schema to convert my string above to the object of type TriggerReponse so that I can avoid the error in this code:

public class TriggerEmailSendingQueryHandler : IRequestHandler<TriggerEmailSendingQuery, TriggerResponse>
    {
        private readonly IConfiguration configuration;

        public TriggerEmailSendingQueryHandler(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public async Task<TriggerResponse> Handle(TriggerEmailSendingQuery request, CancellationToken cancellationToken)
        {
            IConfigurationSection pcConfig = configuration.GetSection(PolicyCenterClient.PC_CONFIG_SECTION);

            var triggerResponse = await FunctionReturningReponse();

            return triggerResponse;
        }
    }

Because when I return triggerResponse which is a string returned from the first function I get this error: Cannot implicitly convert type 'string' to 'Services.Schema.TriggerResponse' [Application]

Alex T
  • 3,529
  • 12
  • 56
  • 105
  • 1
    _"I am trying to understand how converting strings to objects works in C#"_ - C# itself does not define any `String`-to-`Object` conversions; that's done by libraries, such as Newtonsoft.Json, System.Text.Json, and particular _providers_ for `Microsoft.Extensions.Configuration`, for example - and you haven't told us what library or system you're using to do the conversions but it looks like you're using `Microsoft.Extensions.Configuration`. – Dai Sep 13 '22 at 16:30
  • Every `object` has a `ToString` but the base implementation returns the name of the type. – Jodrell Sep 13 '22 at 16:32
  • Yes that is correct I am using `Microsoft.Extensions.Configuration` – Alex T Sep 13 '22 at 16:32
  • `Microsoft.Extensions.Configuration` is not associated with serialization. – Jodrell Sep 13 '22 at 16:34
  • @Jodrell is there a way to create object based on type? Like I am providing this schema type and would create an object based on that, so somethin like `var NewObj = { a: "1", b: "2"}` ? Somehow this approach so far is not working for me so I a mtrying to figure out how I could deal with objects here. – Alex T Sep 13 '22 at 16:35
  • https://stackoverflow.com/questions/7598088/purpose-of-activator-createinstance-with-example – Jodrell Sep 13 '22 at 16:36
  • https://stackoverflow.com/questions/52156484/how-exactly-does-microsoft-extensions-configuration-dependent-on-asp-net-core – Jodrell Sep 13 '22 at 16:42
  • 1
    To be honest, the library being used here is inconsequential, this is a type mismatch – Narish Sep 13 '22 at 16:43

1 Answers1

1

This is a simple type mismatch - you declare that you are going to return a TriggerResponse in the Handle() task, but attempt to return a string

Why not just update the FunctionReturningReponse() to return the proper type? No casts necessary, as to be honest a cast isn't possible here nor is it desirable

public async Task<TriggerResponse> FunctionReturningReponse() {
    string response = "{'ReponseText': 'Some Text'}";
    return new TriggerResponse { ResponseText = response };
}
Narish
  • 607
  • 4
  • 18