C
C#13mo ago
arch_il

✅ Convert from string to Type?

public class Foo
{
public string MyTypeInString = "Foo";
}

public static class SomeFunctions
{
public static MyFunction<T>(T input)
{
//
}
}

public class Program
{
public static void Main()
{
Foo f = new();
SomeFunctions.MyFunction<f.MyTypeInString.ConvertToType()>(f);
}
}
public class Foo
{
public string MyTypeInString = "Foo";
}

public static class SomeFunctions
{
public static MyFunction<T>(T input)
{
//
}
}

public class Program
{
public static void Main()
{
Foo f = new();
SomeFunctions.MyFunction<f.MyTypeInString.ConvertToType()>(f);
}
}
How do I get that ConvertToType(this string) function? I believe it should be possible.
3 Replies
ero
ero13mo ago
it'll definitely never be possible like that you would need to invoke the MyFunction method entirely through reflection you can't pass an instance of Type as a generic argument
Thinker
Thinker13mo ago
Generics are purely for compile-time types
arch_il
arch_il13mo ago
aah i see. thanks for help
Want results from more Discord servers?
Add your server
More Posts
Returning a value after a insert statementThis is the code im running in DotNet: ```c++ var testQuery = $@" INSERT INTO [{databaseName}].[db❔ ASP.NET Core port remaining :5000 even after setting it to something else in launchSettings.jsonTitle pretty much explains it, it also runs on :38120 on my local computer but not on a release buil✅ ASP.NET Web API cant be reached: (SOLVED: Setup reverse proxy)im making a small web api using ASP.NET and im having issues with running my code on a server to tes✅ How to get class structure with types?I have following class: ```cpp public class ReportTemplatesListArguments { [JsonProperty( Proper❔ implementing api authentication in asp.netAny .net developers who would be willing to help me implement an api with token authentication?DotNet and Dapper. Incorrect syntax near ','.Hello folks, Im trying to execute a sql query: ```c++ [HttpPost] public async Task<ActionRe❔ Reading from ClientWebSocket with BinaryReaderI am building an app to work as a gateway between a consumer connecting to a .NET websocket server a❔ How to use a readonly property for binding [AvalonEdit]?Hello, I have created a behavior for AvalonEdit following the accepted answer here https://stackoverReturn NotFound with detailsHello. I have a asp.net core Web API with controllers and I want to return for example a NotFound Er❔ Is building a DM/messaging system as simple as it seems?With signalr, that is. Is it as simple as just setting up signalr and persisting messages and then