Share via

Generic method how to use variables

prof cegep 41 Reputation points
2023-03-02T00:23:57.4633333+00:00

Hi,

I wonder how to assign values in a generic method :

Calls :

this.FormaterZones<trelEmplacement>(donnees);
this.FormaterZones<trelSpecification>(info);

Method that not wokring :

    private void FormaterZones<T>(T infos)
    {
        if (typeof(T) == typeof(trelEmplacement))
        {
            infos.emplacementDescriptionComplete = infos.emplacementDescriptionComplete.PremierMotLettreMajuscule();
        }
        else
        {
            if (typeof(T) == typeof(trelSpecification))
            {
                infos.specificationDescriptionComplete = infos.emplacementDescriptionComplete.PremierMotLettreMajuscule();
            }
        }

    }


public class trelSpecification
{
    [Key]
    public Guid trelSpecificationsId { get; set; }

    public int positionLigne { get; set; }

    [StringLength(1000)]
    public string? specificationDescriptionCompleteFr { get; set; }

    [StringLength(1000)]
    public string? specificationDescriptionCompleteEn { get; set; }

    [StringLength(1000)]
    public string? specificationDescriptionCompleteXy { get; set; }

}

Any suggestion? Thank you

Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Jack J Jun 25,306 Reputation points
2023-03-02T02:13:03.0633333+00:00

@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:

User's image

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.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.