Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
@prof cegep, Welcome to Microsoft Q&A, you could try to use Convert.ChangeType method to cast generic type parameter to a specific type.
Here is a code example you could refer to.
internal class Program
{
static void Main(string[] args)
{
trelEmplacement tr1=new trelEmplacement();
trelSpecification tr2 = new trelSpecification();
Program p=new Program();
p.FormaterZones(tr1);
p.FormaterZones(tr2);
Console.WriteLine(tr1.emplacementDescriptionComplete);
Console.WriteLine(tr2.specificationDescriptionComplete);
}
private void FormaterZones<T>(T infos)
{
if (typeof(T) == typeof(trelEmplacement))
{
trelEmplacement tr= (trelEmplacement)Convert.ChangeType(infos, typeof(trelEmplacement));
tr.emplacementDescriptionComplete = "success";
}
else
{
if (typeof(T) == typeof(trelSpecification))
{
trelSpecification tr= (trelSpecification)Convert.ChangeType(infos, typeof(trelSpecification));
tr.specificationDescriptionComplete = "failed";
}
}
}
}
public class trelEmplacement
{
public string emplacementDescriptionComplete { get; set; }
}
public class trelSpecification
{
public string specificationDescriptionComplete { get; set; }
}
Tested result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.