How to return a value of passed in property of generated object in c#












1















I have a function that returns a particular type. Ex. FaxFile. In that object there are several different formats to download ex: PDF, LargeJpg, SmallJpg, etc.



I can call the download a pdf like this.



public FaxFile DownloadFaxPDFById(int faxId)
{
return DownloadFaxById(faxId).Pdf;
}


What I am wanting to do is be able to pass in the property of the object ex. LargeJpg Format to download.



Sudo Code



public FaxFile DownloadFaxTypeById(int faxId, property)
{
return DownloadFaxById(faxId).property;
}


How do I do this?










share|improve this question























  • why not use use switch case based on property?

    – Karthik Ganesan
    Jan 2 at 14:45











  • You can't use delegates for this, but you can use lambdas.

    – Yola
    Jan 2 at 14:46






  • 2





    @Yola Lambdas are one of the ways of creating delegates. It's literally impossible to use lambdas without using delegates.

    – Servy
    Jan 2 at 14:51






  • 1





    What is 'Sudo Code'? Only executable by admins? :s

    – nilsK
    Jan 2 at 15:00











  • Are there only FaxFile or do you have also something like PDFFaxFile or LargeJpgFaxFile?

    – nosale
    Jan 2 at 15:00
















1















I have a function that returns a particular type. Ex. FaxFile. In that object there are several different formats to download ex: PDF, LargeJpg, SmallJpg, etc.



I can call the download a pdf like this.



public FaxFile DownloadFaxPDFById(int faxId)
{
return DownloadFaxById(faxId).Pdf;
}


What I am wanting to do is be able to pass in the property of the object ex. LargeJpg Format to download.



Sudo Code



public FaxFile DownloadFaxTypeById(int faxId, property)
{
return DownloadFaxById(faxId).property;
}


How do I do this?










share|improve this question























  • why not use use switch case based on property?

    – Karthik Ganesan
    Jan 2 at 14:45











  • You can't use delegates for this, but you can use lambdas.

    – Yola
    Jan 2 at 14:46






  • 2





    @Yola Lambdas are one of the ways of creating delegates. It's literally impossible to use lambdas without using delegates.

    – Servy
    Jan 2 at 14:51






  • 1





    What is 'Sudo Code'? Only executable by admins? :s

    – nilsK
    Jan 2 at 15:00











  • Are there only FaxFile or do you have also something like PDFFaxFile or LargeJpgFaxFile?

    – nosale
    Jan 2 at 15:00














1












1








1








I have a function that returns a particular type. Ex. FaxFile. In that object there are several different formats to download ex: PDF, LargeJpg, SmallJpg, etc.



I can call the download a pdf like this.



public FaxFile DownloadFaxPDFById(int faxId)
{
return DownloadFaxById(faxId).Pdf;
}


What I am wanting to do is be able to pass in the property of the object ex. LargeJpg Format to download.



Sudo Code



public FaxFile DownloadFaxTypeById(int faxId, property)
{
return DownloadFaxById(faxId).property;
}


How do I do this?










share|improve this question














I have a function that returns a particular type. Ex. FaxFile. In that object there are several different formats to download ex: PDF, LargeJpg, SmallJpg, etc.



I can call the download a pdf like this.



public FaxFile DownloadFaxPDFById(int faxId)
{
return DownloadFaxById(faxId).Pdf;
}


What I am wanting to do is be able to pass in the property of the object ex. LargeJpg Format to download.



Sudo Code



public FaxFile DownloadFaxTypeById(int faxId, property)
{
return DownloadFaxById(faxId).property;
}


How do I do this?







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 14:43









DemodaveDemodave

3,52642942




3,52642942













  • why not use use switch case based on property?

    – Karthik Ganesan
    Jan 2 at 14:45











  • You can't use delegates for this, but you can use lambdas.

    – Yola
    Jan 2 at 14:46






  • 2





    @Yola Lambdas are one of the ways of creating delegates. It's literally impossible to use lambdas without using delegates.

    – Servy
    Jan 2 at 14:51






  • 1





    What is 'Sudo Code'? Only executable by admins? :s

    – nilsK
    Jan 2 at 15:00











  • Are there only FaxFile or do you have also something like PDFFaxFile or LargeJpgFaxFile?

    – nosale
    Jan 2 at 15:00



















  • why not use use switch case based on property?

    – Karthik Ganesan
    Jan 2 at 14:45











  • You can't use delegates for this, but you can use lambdas.

    – Yola
    Jan 2 at 14:46






  • 2





    @Yola Lambdas are one of the ways of creating delegates. It's literally impossible to use lambdas without using delegates.

    – Servy
    Jan 2 at 14:51






  • 1





    What is 'Sudo Code'? Only executable by admins? :s

    – nilsK
    Jan 2 at 15:00











  • Are there only FaxFile or do you have also something like PDFFaxFile or LargeJpgFaxFile?

    – nosale
    Jan 2 at 15:00

















why not use use switch case based on property?

– Karthik Ganesan
Jan 2 at 14:45





why not use use switch case based on property?

– Karthik Ganesan
Jan 2 at 14:45













You can't use delegates for this, but you can use lambdas.

– Yola
Jan 2 at 14:46





You can't use delegates for this, but you can use lambdas.

– Yola
Jan 2 at 14:46




2




2





@Yola Lambdas are one of the ways of creating delegates. It's literally impossible to use lambdas without using delegates.

– Servy
Jan 2 at 14:51





@Yola Lambdas are one of the ways of creating delegates. It's literally impossible to use lambdas without using delegates.

– Servy
Jan 2 at 14:51




1




1





What is 'Sudo Code'? Only executable by admins? :s

– nilsK
Jan 2 at 15:00





What is 'Sudo Code'? Only executable by admins? :s

– nilsK
Jan 2 at 15:00













Are there only FaxFile or do you have also something like PDFFaxFile or LargeJpgFaxFile?

– nosale
Jan 2 at 15:00





Are there only FaxFile or do you have also something like PDFFaxFile or LargeJpgFaxFile?

– nosale
Jan 2 at 15:00












5 Answers
5






active

oldest

votes


















0














I would recommend to create an enum for this and pass it to your function.

Inside your function simple use switch.

This means more programming, but the usage of your function is much clearer.

This has also the benefit that you later can change the code how you obtain each format without taking care of the consumer of your function.



enum FaxFileType
{
PDF,
LargeJpg,
SmallJpg,
...
}




public FaxFile DownloadFaxById(int faxId, FaxFileType faxFileType)
{
switch (faxFileType)
{
case FaxFileType.PDF:
return DownloadFaxById(faxId).PDF;
case FaxFileType.LargeJpg:
return DownloadFaxById(faxId).LargeJpg;
case FaxFileType.SmallJpg:
return DownloadFaxById(faxId).SmallJpg;
...
default:
throw new NotSupportedException();
}
}





share|improve this answer


























  • This one appears to be the simplest solution. As this library I am using I don't think will change I should choose this.

    – Demodave
    Jan 4 at 13:47



















2














you can use reflection.



var resultObj = DownloadFaxById(faxId);
var result = resultObj.GetType().GetProperty("<propertyName>").GetValue(resultObj);


please note that you need to cast result to appropriate object






share|improve this answer
























  • I would possibly choose this one if the object I had would changed

    – Demodave
    Jan 4 at 13:48





















1














You don't want to do this at the first place. Just use DownloadFaxById(faxId).Pdf/LargeJpg/...; in the call place or, if you don't want to expose class returned by DownloadFaxById either subclass or use aggregation and expose Pdf, LargeJpg, ... in this new class.






share|improve this answer































    1














    Another way of doing this besides those already posted as answers is to use lambda expressions. Not sure what's the type of the downloaded object, so replace DownloadedObjectType with your own.



    public FaxFile DownloadFaxTypeById(int faxId, Expression<Func<DownloadedObjectType, FaxFile>> expression) {

    if (!(expression.Body is MemberExpression)) {
    throw new ArgumentException("Invalid expression");
    }

    return expression.Compile().Invoke(DownloadFaxById(faxId));
    }


    You then call it as



    DownloadFaxTypeById(faxId, obj => obj.Pdf)


    However looks much uglier than simply calling



    DownloadFaxTypeById(faxId).Pdf


    Except for maybe providing you some control over what properties can the caller retrieve, limiting them to that specific return type and only those that are actually available for that type. This way for a subset of possible errors (like referencing non-existing property) you get compile time errors rather than all runtime as in cases using reflection.






    share|improve this answer





















    • 1





      I agree that DownloadFaxTypeById(faxId).Pdf is much simpler, would interesting to see why this is not a viable solution.

      – Scrobi
      Jan 2 at 15:06











    • I started down this path, but I too thought it had to be simpler

      – Demodave
      Jan 4 at 13:49



















    0














    You should probably just use a switch statement for each format type you support from an enum. However if you really want to use reflection, you can so it as follows:



    void Main()
    {
    var foo = new Foo();

    foo.GetType().GetProperty("MyProp").SetValue(foo, "Test");
    var val = foo.GetType().GetProperty("MyProp").GetValue(foo);

    Console.WriteLine(val);
    }

    public class Foo
    {
    public String MyProp { get; set; }
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008316%2fhow-to-return-a-value-of-passed-in-property-of-generated-object-in-c-sharp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I would recommend to create an enum for this and pass it to your function.

      Inside your function simple use switch.

      This means more programming, but the usage of your function is much clearer.

      This has also the benefit that you later can change the code how you obtain each format without taking care of the consumer of your function.



      enum FaxFileType
      {
      PDF,
      LargeJpg,
      SmallJpg,
      ...
      }




      public FaxFile DownloadFaxById(int faxId, FaxFileType faxFileType)
      {
      switch (faxFileType)
      {
      case FaxFileType.PDF:
      return DownloadFaxById(faxId).PDF;
      case FaxFileType.LargeJpg:
      return DownloadFaxById(faxId).LargeJpg;
      case FaxFileType.SmallJpg:
      return DownloadFaxById(faxId).SmallJpg;
      ...
      default:
      throw new NotSupportedException();
      }
      }





      share|improve this answer


























      • This one appears to be the simplest solution. As this library I am using I don't think will change I should choose this.

        – Demodave
        Jan 4 at 13:47
















      0














      I would recommend to create an enum for this and pass it to your function.

      Inside your function simple use switch.

      This means more programming, but the usage of your function is much clearer.

      This has also the benefit that you later can change the code how you obtain each format without taking care of the consumer of your function.



      enum FaxFileType
      {
      PDF,
      LargeJpg,
      SmallJpg,
      ...
      }




      public FaxFile DownloadFaxById(int faxId, FaxFileType faxFileType)
      {
      switch (faxFileType)
      {
      case FaxFileType.PDF:
      return DownloadFaxById(faxId).PDF;
      case FaxFileType.LargeJpg:
      return DownloadFaxById(faxId).LargeJpg;
      case FaxFileType.SmallJpg:
      return DownloadFaxById(faxId).SmallJpg;
      ...
      default:
      throw new NotSupportedException();
      }
      }





      share|improve this answer


























      • This one appears to be the simplest solution. As this library I am using I don't think will change I should choose this.

        – Demodave
        Jan 4 at 13:47














      0












      0








      0







      I would recommend to create an enum for this and pass it to your function.

      Inside your function simple use switch.

      This means more programming, but the usage of your function is much clearer.

      This has also the benefit that you later can change the code how you obtain each format without taking care of the consumer of your function.



      enum FaxFileType
      {
      PDF,
      LargeJpg,
      SmallJpg,
      ...
      }




      public FaxFile DownloadFaxById(int faxId, FaxFileType faxFileType)
      {
      switch (faxFileType)
      {
      case FaxFileType.PDF:
      return DownloadFaxById(faxId).PDF;
      case FaxFileType.LargeJpg:
      return DownloadFaxById(faxId).LargeJpg;
      case FaxFileType.SmallJpg:
      return DownloadFaxById(faxId).SmallJpg;
      ...
      default:
      throw new NotSupportedException();
      }
      }





      share|improve this answer















      I would recommend to create an enum for this and pass it to your function.

      Inside your function simple use switch.

      This means more programming, but the usage of your function is much clearer.

      This has also the benefit that you later can change the code how you obtain each format without taking care of the consumer of your function.



      enum FaxFileType
      {
      PDF,
      LargeJpg,
      SmallJpg,
      ...
      }




      public FaxFile DownloadFaxById(int faxId, FaxFileType faxFileType)
      {
      switch (faxFileType)
      {
      case FaxFileType.PDF:
      return DownloadFaxById(faxId).PDF;
      case FaxFileType.LargeJpg:
      return DownloadFaxById(faxId).LargeJpg;
      case FaxFileType.SmallJpg:
      return DownloadFaxById(faxId).SmallJpg;
      ...
      default:
      throw new NotSupportedException();
      }
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 2 at 15:34

























      answered Jan 2 at 15:17









      nosalenosale

      672212




      672212













      • This one appears to be the simplest solution. As this library I am using I don't think will change I should choose this.

        – Demodave
        Jan 4 at 13:47



















      • This one appears to be the simplest solution. As this library I am using I don't think will change I should choose this.

        – Demodave
        Jan 4 at 13:47

















      This one appears to be the simplest solution. As this library I am using I don't think will change I should choose this.

      – Demodave
      Jan 4 at 13:47





      This one appears to be the simplest solution. As this library I am using I don't think will change I should choose this.

      – Demodave
      Jan 4 at 13:47













      2














      you can use reflection.



      var resultObj = DownloadFaxById(faxId);
      var result = resultObj.GetType().GetProperty("<propertyName>").GetValue(resultObj);


      please note that you need to cast result to appropriate object






      share|improve this answer
























      • I would possibly choose this one if the object I had would changed

        – Demodave
        Jan 4 at 13:48


















      2














      you can use reflection.



      var resultObj = DownloadFaxById(faxId);
      var result = resultObj.GetType().GetProperty("<propertyName>").GetValue(resultObj);


      please note that you need to cast result to appropriate object






      share|improve this answer
























      • I would possibly choose this one if the object I had would changed

        – Demodave
        Jan 4 at 13:48
















      2












      2








      2







      you can use reflection.



      var resultObj = DownloadFaxById(faxId);
      var result = resultObj.GetType().GetProperty("<propertyName>").GetValue(resultObj);


      please note that you need to cast result to appropriate object






      share|improve this answer













      you can use reflection.



      var resultObj = DownloadFaxById(faxId);
      var result = resultObj.GetType().GetProperty("<propertyName>").GetValue(resultObj);


      please note that you need to cast result to appropriate object







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 2 at 14:50









      Derviş KayımbaşıoğluDerviş Kayımbaşıoğlu

      15.6k22042




      15.6k22042













      • I would possibly choose this one if the object I had would changed

        – Demodave
        Jan 4 at 13:48





















      • I would possibly choose this one if the object I had would changed

        – Demodave
        Jan 4 at 13:48



















      I would possibly choose this one if the object I had would changed

      – Demodave
      Jan 4 at 13:48







      I would possibly choose this one if the object I had would changed

      – Demodave
      Jan 4 at 13:48













      1














      You don't want to do this at the first place. Just use DownloadFaxById(faxId).Pdf/LargeJpg/...; in the call place or, if you don't want to expose class returned by DownloadFaxById either subclass or use aggregation and expose Pdf, LargeJpg, ... in this new class.






      share|improve this answer




























        1














        You don't want to do this at the first place. Just use DownloadFaxById(faxId).Pdf/LargeJpg/...; in the call place or, if you don't want to expose class returned by DownloadFaxById either subclass or use aggregation and expose Pdf, LargeJpg, ... in this new class.






        share|improve this answer


























          1












          1








          1







          You don't want to do this at the first place. Just use DownloadFaxById(faxId).Pdf/LargeJpg/...; in the call place or, if you don't want to expose class returned by DownloadFaxById either subclass or use aggregation and expose Pdf, LargeJpg, ... in this new class.






          share|improve this answer













          You don't want to do this at the first place. Just use DownloadFaxById(faxId).Pdf/LargeJpg/...; in the call place or, if you don't want to expose class returned by DownloadFaxById either subclass or use aggregation and expose Pdf, LargeJpg, ... in this new class.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 15:09









          YolaYola

          11.3k64672




          11.3k64672























              1














              Another way of doing this besides those already posted as answers is to use lambda expressions. Not sure what's the type of the downloaded object, so replace DownloadedObjectType with your own.



              public FaxFile DownloadFaxTypeById(int faxId, Expression<Func<DownloadedObjectType, FaxFile>> expression) {

              if (!(expression.Body is MemberExpression)) {
              throw new ArgumentException("Invalid expression");
              }

              return expression.Compile().Invoke(DownloadFaxById(faxId));
              }


              You then call it as



              DownloadFaxTypeById(faxId, obj => obj.Pdf)


              However looks much uglier than simply calling



              DownloadFaxTypeById(faxId).Pdf


              Except for maybe providing you some control over what properties can the caller retrieve, limiting them to that specific return type and only those that are actually available for that type. This way for a subset of possible errors (like referencing non-existing property) you get compile time errors rather than all runtime as in cases using reflection.






              share|improve this answer





















              • 1





                I agree that DownloadFaxTypeById(faxId).Pdf is much simpler, would interesting to see why this is not a viable solution.

                – Scrobi
                Jan 2 at 15:06











              • I started down this path, but I too thought it had to be simpler

                – Demodave
                Jan 4 at 13:49
















              1














              Another way of doing this besides those already posted as answers is to use lambda expressions. Not sure what's the type of the downloaded object, so replace DownloadedObjectType with your own.



              public FaxFile DownloadFaxTypeById(int faxId, Expression<Func<DownloadedObjectType, FaxFile>> expression) {

              if (!(expression.Body is MemberExpression)) {
              throw new ArgumentException("Invalid expression");
              }

              return expression.Compile().Invoke(DownloadFaxById(faxId));
              }


              You then call it as



              DownloadFaxTypeById(faxId, obj => obj.Pdf)


              However looks much uglier than simply calling



              DownloadFaxTypeById(faxId).Pdf


              Except for maybe providing you some control over what properties can the caller retrieve, limiting them to that specific return type and only those that are actually available for that type. This way for a subset of possible errors (like referencing non-existing property) you get compile time errors rather than all runtime as in cases using reflection.






              share|improve this answer





















              • 1





                I agree that DownloadFaxTypeById(faxId).Pdf is much simpler, would interesting to see why this is not a viable solution.

                – Scrobi
                Jan 2 at 15:06











              • I started down this path, but I too thought it had to be simpler

                – Demodave
                Jan 4 at 13:49














              1












              1








              1







              Another way of doing this besides those already posted as answers is to use lambda expressions. Not sure what's the type of the downloaded object, so replace DownloadedObjectType with your own.



              public FaxFile DownloadFaxTypeById(int faxId, Expression<Func<DownloadedObjectType, FaxFile>> expression) {

              if (!(expression.Body is MemberExpression)) {
              throw new ArgumentException("Invalid expression");
              }

              return expression.Compile().Invoke(DownloadFaxById(faxId));
              }


              You then call it as



              DownloadFaxTypeById(faxId, obj => obj.Pdf)


              However looks much uglier than simply calling



              DownloadFaxTypeById(faxId).Pdf


              Except for maybe providing you some control over what properties can the caller retrieve, limiting them to that specific return type and only those that are actually available for that type. This way for a subset of possible errors (like referencing non-existing property) you get compile time errors rather than all runtime as in cases using reflection.






              share|improve this answer















              Another way of doing this besides those already posted as answers is to use lambda expressions. Not sure what's the type of the downloaded object, so replace DownloadedObjectType with your own.



              public FaxFile DownloadFaxTypeById(int faxId, Expression<Func<DownloadedObjectType, FaxFile>> expression) {

              if (!(expression.Body is MemberExpression)) {
              throw new ArgumentException("Invalid expression");
              }

              return expression.Compile().Invoke(DownloadFaxById(faxId));
              }


              You then call it as



              DownloadFaxTypeById(faxId, obj => obj.Pdf)


              However looks much uglier than simply calling



              DownloadFaxTypeById(faxId).Pdf


              Except for maybe providing you some control over what properties can the caller retrieve, limiting them to that specific return type and only those that are actually available for that type. This way for a subset of possible errors (like referencing non-existing property) you get compile time errors rather than all runtime as in cases using reflection.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 2 at 15:16

























              answered Jan 2 at 14:57









              ImantasImantas

              1,103918




              1,103918








              • 1





                I agree that DownloadFaxTypeById(faxId).Pdf is much simpler, would interesting to see why this is not a viable solution.

                – Scrobi
                Jan 2 at 15:06











              • I started down this path, but I too thought it had to be simpler

                – Demodave
                Jan 4 at 13:49














              • 1





                I agree that DownloadFaxTypeById(faxId).Pdf is much simpler, would interesting to see why this is not a viable solution.

                – Scrobi
                Jan 2 at 15:06











              • I started down this path, but I too thought it had to be simpler

                – Demodave
                Jan 4 at 13:49








              1




              1





              I agree that DownloadFaxTypeById(faxId).Pdf is much simpler, would interesting to see why this is not a viable solution.

              – Scrobi
              Jan 2 at 15:06





              I agree that DownloadFaxTypeById(faxId).Pdf is much simpler, would interesting to see why this is not a viable solution.

              – Scrobi
              Jan 2 at 15:06













              I started down this path, but I too thought it had to be simpler

              – Demodave
              Jan 4 at 13:49





              I started down this path, but I too thought it had to be simpler

              – Demodave
              Jan 4 at 13:49











              0














              You should probably just use a switch statement for each format type you support from an enum. However if you really want to use reflection, you can so it as follows:



              void Main()
              {
              var foo = new Foo();

              foo.GetType().GetProperty("MyProp").SetValue(foo, "Test");
              var val = foo.GetType().GetProperty("MyProp").GetValue(foo);

              Console.WriteLine(val);
              }

              public class Foo
              {
              public String MyProp { get; set; }
              }





              share|improve this answer




























                0














                You should probably just use a switch statement for each format type you support from an enum. However if you really want to use reflection, you can so it as follows:



                void Main()
                {
                var foo = new Foo();

                foo.GetType().GetProperty("MyProp").SetValue(foo, "Test");
                var val = foo.GetType().GetProperty("MyProp").GetValue(foo);

                Console.WriteLine(val);
                }

                public class Foo
                {
                public String MyProp { get; set; }
                }





                share|improve this answer


























                  0












                  0








                  0







                  You should probably just use a switch statement for each format type you support from an enum. However if you really want to use reflection, you can so it as follows:



                  void Main()
                  {
                  var foo = new Foo();

                  foo.GetType().GetProperty("MyProp").SetValue(foo, "Test");
                  var val = foo.GetType().GetProperty("MyProp").GetValue(foo);

                  Console.WriteLine(val);
                  }

                  public class Foo
                  {
                  public String MyProp { get; set; }
                  }





                  share|improve this answer













                  You should probably just use a switch statement for each format type you support from an enum. However if you really want to use reflection, you can so it as follows:



                  void Main()
                  {
                  var foo = new Foo();

                  foo.GetType().GetProperty("MyProp").SetValue(foo, "Test");
                  var val = foo.GetType().GetProperty("MyProp").GetValue(foo);

                  Console.WriteLine(val);
                  }

                  public class Foo
                  {
                  public String MyProp { get; set; }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 14:51









                  Carlo BosCarlo Bos

                  748513




                  748513






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008316%2fhow-to-return-a-value-of-passed-in-property-of-generated-object-in-c-sharp%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter