Answers for "cannot convert from string to type T"

C#
1

cannot convert from string to type T

T newT1 = (T)(object)"some text";
string newT2 = (string)(object)t;
Posted by: Guest on May-01-2020
0

cannot convert from string to type T

public TResult ParseSomething<TResult>(ParseContext context)
{
    if (typeof(TResult) == typeof(string))
    {
        var token = context.ParseNextToken();
        string parsedString = token.ParseToDotnetString();
        return Unsafe.As<string, TResult>(ref parsedString);
    }
    else if (typeof(TResult) == typeof(int))
    {
        var token = context.ParseNextToken();
        int parsedInt32 = token.ParseToDotnetInt32();
        // This will not box which might be critical to performance
        return Unsafe.As<int, TResult>(ref parsedInt32); 
    }
    // other cases omitted for brevity's sake
}
Posted by: Guest on April-23-2022

Code answers related to "cannot convert from string to type T"

C# Answers by Framework

Browse Popular Code Answers by Language