How to call generic property from another generic method












0















Here is my generic method from which i want to return the class object



 public class TestBase
{
public T NavigateandReturntheObject<T>() where T : new()
{
//do navigate to page stuff and return the page object

//previously it was - return new T();

//Now i want to do something like this
return PageObjectBase<T>.PageObject;
}
}


Above method calling the below static generic class which will handle object creation of a particular class



 public static class PageObjectBase<T> where T : class, new()
{
private static T singleTonObject;
public static T PageObject
{
get
{
return InstanceCreation();
}
}

public static T InstanceCreation()
{
if (singleTonObject == null)
{
singleTonObject = new T();
}

return singleTonObject;
}

}


How can i call the PageObject property from my test base class please advice.
Note : I have searched forum and find answers relevant to generic method to another generic method calling.The same is achieved by reflection.Can we use reflection in my case too? If so how can we do it.










share|improve this question



























    0















    Here is my generic method from which i want to return the class object



     public class TestBase
    {
    public T NavigateandReturntheObject<T>() where T : new()
    {
    //do navigate to page stuff and return the page object

    //previously it was - return new T();

    //Now i want to do something like this
    return PageObjectBase<T>.PageObject;
    }
    }


    Above method calling the below static generic class which will handle object creation of a particular class



     public static class PageObjectBase<T> where T : class, new()
    {
    private static T singleTonObject;
    public static T PageObject
    {
    get
    {
    return InstanceCreation();
    }
    }

    public static T InstanceCreation()
    {
    if (singleTonObject == null)
    {
    singleTonObject = new T();
    }

    return singleTonObject;
    }

    }


    How can i call the PageObject property from my test base class please advice.
    Note : I have searched forum and find answers relevant to generic method to another generic method calling.The same is achieved by reflection.Can we use reflection in my case too? If so how can we do it.










    share|improve this question

























      0












      0








      0








      Here is my generic method from which i want to return the class object



       public class TestBase
      {
      public T NavigateandReturntheObject<T>() where T : new()
      {
      //do navigate to page stuff and return the page object

      //previously it was - return new T();

      //Now i want to do something like this
      return PageObjectBase<T>.PageObject;
      }
      }


      Above method calling the below static generic class which will handle object creation of a particular class



       public static class PageObjectBase<T> where T : class, new()
      {
      private static T singleTonObject;
      public static T PageObject
      {
      get
      {
      return InstanceCreation();
      }
      }

      public static T InstanceCreation()
      {
      if (singleTonObject == null)
      {
      singleTonObject = new T();
      }

      return singleTonObject;
      }

      }


      How can i call the PageObject property from my test base class please advice.
      Note : I have searched forum and find answers relevant to generic method to another generic method calling.The same is achieved by reflection.Can we use reflection in my case too? If so how can we do it.










      share|improve this question














      Here is my generic method from which i want to return the class object



       public class TestBase
      {
      public T NavigateandReturntheObject<T>() where T : new()
      {
      //do navigate to page stuff and return the page object

      //previously it was - return new T();

      //Now i want to do something like this
      return PageObjectBase<T>.PageObject;
      }
      }


      Above method calling the below static generic class which will handle object creation of a particular class



       public static class PageObjectBase<T> where T : class, new()
      {
      private static T singleTonObject;
      public static T PageObject
      {
      get
      {
      return InstanceCreation();
      }
      }

      public static T InstanceCreation()
      {
      if (singleTonObject == null)
      {
      singleTonObject = new T();
      }

      return singleTonObject;
      }

      }


      How can i call the PageObject property from my test base class please advice.
      Note : I have searched forum and find answers relevant to generic method to another generic method calling.The same is achieved by reflection.Can we use reflection in my case too? If so how can we do it.







      c# generics system.reflection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 8:30









      rahulrahul

      133




      133
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can add another constraint 'class' to NavigateandReturntheObject



          public T NavigateandReturntheObject<T>() where T : class,new()


          Complete Code.



          public class TestBase
          {
          public T NavigateandReturntheObject<T>() where T : class,new()
          {
          //do navigate to page stuff and return the page object

          //previously it was - return new T();

          //Now i want to do something like this
          return PageObjectBase<T>.PageObject;
          }
          }


          Demo Code



          public class TestClass
          {
          public string Name{get;set;}
          public TestClass()
          {
          Name = "Dummy Name";
          }
          }

          var testBase = new TestBase();
          var sample = testBase.NavigateandReturntheObject<TestClass>();
          Console.WriteLine(sample.Name);


          Output



          Dummy Name





          share|improve this answer


























          • The contraint class would not be able to able to achieve what the OP wants. Although adding a new contrain is the way to go. Check my comment where I describe the contrain needed.

            – bradbury9
            Dec 31 '18 at 8:46











          • @bradbury9 am curious on why above won't work. I have added sample client code now, with output. Why do you think above wont work ?

            – Anu Viswan
            Dec 31 '18 at 9:06






          • 1





            Sorry, misread the question

            – bradbury9
            Dec 31 '18 at 9:07











          • This works .I didn't know this is as simple as that.So the problem is that the constraint in both the places(calling method and the method it calls) should be same.Thanks to both of you for looking into this

            – rahul
            Dec 31 '18 at 9:35











          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%2f53985246%2fhow-to-call-generic-property-from-another-generic-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can add another constraint 'class' to NavigateandReturntheObject



          public T NavigateandReturntheObject<T>() where T : class,new()


          Complete Code.



          public class TestBase
          {
          public T NavigateandReturntheObject<T>() where T : class,new()
          {
          //do navigate to page stuff and return the page object

          //previously it was - return new T();

          //Now i want to do something like this
          return PageObjectBase<T>.PageObject;
          }
          }


          Demo Code



          public class TestClass
          {
          public string Name{get;set;}
          public TestClass()
          {
          Name = "Dummy Name";
          }
          }

          var testBase = new TestBase();
          var sample = testBase.NavigateandReturntheObject<TestClass>();
          Console.WriteLine(sample.Name);


          Output



          Dummy Name





          share|improve this answer


























          • The contraint class would not be able to able to achieve what the OP wants. Although adding a new contrain is the way to go. Check my comment where I describe the contrain needed.

            – bradbury9
            Dec 31 '18 at 8:46











          • @bradbury9 am curious on why above won't work. I have added sample client code now, with output. Why do you think above wont work ?

            – Anu Viswan
            Dec 31 '18 at 9:06






          • 1





            Sorry, misread the question

            – bradbury9
            Dec 31 '18 at 9:07











          • This works .I didn't know this is as simple as that.So the problem is that the constraint in both the places(calling method and the method it calls) should be same.Thanks to both of you for looking into this

            – rahul
            Dec 31 '18 at 9:35
















          0














          You can add another constraint 'class' to NavigateandReturntheObject



          public T NavigateandReturntheObject<T>() where T : class,new()


          Complete Code.



          public class TestBase
          {
          public T NavigateandReturntheObject<T>() where T : class,new()
          {
          //do navigate to page stuff and return the page object

          //previously it was - return new T();

          //Now i want to do something like this
          return PageObjectBase<T>.PageObject;
          }
          }


          Demo Code



          public class TestClass
          {
          public string Name{get;set;}
          public TestClass()
          {
          Name = "Dummy Name";
          }
          }

          var testBase = new TestBase();
          var sample = testBase.NavigateandReturntheObject<TestClass>();
          Console.WriteLine(sample.Name);


          Output



          Dummy Name





          share|improve this answer


























          • The contraint class would not be able to able to achieve what the OP wants. Although adding a new contrain is the way to go. Check my comment where I describe the contrain needed.

            – bradbury9
            Dec 31 '18 at 8:46











          • @bradbury9 am curious on why above won't work. I have added sample client code now, with output. Why do you think above wont work ?

            – Anu Viswan
            Dec 31 '18 at 9:06






          • 1





            Sorry, misread the question

            – bradbury9
            Dec 31 '18 at 9:07











          • This works .I didn't know this is as simple as that.So the problem is that the constraint in both the places(calling method and the method it calls) should be same.Thanks to both of you for looking into this

            – rahul
            Dec 31 '18 at 9:35














          0












          0








          0







          You can add another constraint 'class' to NavigateandReturntheObject



          public T NavigateandReturntheObject<T>() where T : class,new()


          Complete Code.



          public class TestBase
          {
          public T NavigateandReturntheObject<T>() where T : class,new()
          {
          //do navigate to page stuff and return the page object

          //previously it was - return new T();

          //Now i want to do something like this
          return PageObjectBase<T>.PageObject;
          }
          }


          Demo Code



          public class TestClass
          {
          public string Name{get;set;}
          public TestClass()
          {
          Name = "Dummy Name";
          }
          }

          var testBase = new TestBase();
          var sample = testBase.NavigateandReturntheObject<TestClass>();
          Console.WriteLine(sample.Name);


          Output



          Dummy Name





          share|improve this answer















          You can add another constraint 'class' to NavigateandReturntheObject



          public T NavigateandReturntheObject<T>() where T : class,new()


          Complete Code.



          public class TestBase
          {
          public T NavigateandReturntheObject<T>() where T : class,new()
          {
          //do navigate to page stuff and return the page object

          //previously it was - return new T();

          //Now i want to do something like this
          return PageObjectBase<T>.PageObject;
          }
          }


          Demo Code



          public class TestClass
          {
          public string Name{get;set;}
          public TestClass()
          {
          Name = "Dummy Name";
          }
          }

          var testBase = new TestBase();
          var sample = testBase.NavigateandReturntheObject<TestClass>();
          Console.WriteLine(sample.Name);


          Output



          Dummy Name






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 31 '18 at 9:03

























          answered Dec 31 '18 at 8:43









          Anu ViswanAnu Viswan

          4,9732524




          4,9732524













          • The contraint class would not be able to able to achieve what the OP wants. Although adding a new contrain is the way to go. Check my comment where I describe the contrain needed.

            – bradbury9
            Dec 31 '18 at 8:46











          • @bradbury9 am curious on why above won't work. I have added sample client code now, with output. Why do you think above wont work ?

            – Anu Viswan
            Dec 31 '18 at 9:06






          • 1





            Sorry, misread the question

            – bradbury9
            Dec 31 '18 at 9:07











          • This works .I didn't know this is as simple as that.So the problem is that the constraint in both the places(calling method and the method it calls) should be same.Thanks to both of you for looking into this

            – rahul
            Dec 31 '18 at 9:35



















          • The contraint class would not be able to able to achieve what the OP wants. Although adding a new contrain is the way to go. Check my comment where I describe the contrain needed.

            – bradbury9
            Dec 31 '18 at 8:46











          • @bradbury9 am curious on why above won't work. I have added sample client code now, with output. Why do you think above wont work ?

            – Anu Viswan
            Dec 31 '18 at 9:06






          • 1





            Sorry, misread the question

            – bradbury9
            Dec 31 '18 at 9:07











          • This works .I didn't know this is as simple as that.So the problem is that the constraint in both the places(calling method and the method it calls) should be same.Thanks to both of you for looking into this

            – rahul
            Dec 31 '18 at 9:35

















          The contraint class would not be able to able to achieve what the OP wants. Although adding a new contrain is the way to go. Check my comment where I describe the contrain needed.

          – bradbury9
          Dec 31 '18 at 8:46





          The contraint class would not be able to able to achieve what the OP wants. Although adding a new contrain is the way to go. Check my comment where I describe the contrain needed.

          – bradbury9
          Dec 31 '18 at 8:46













          @bradbury9 am curious on why above won't work. I have added sample client code now, with output. Why do you think above wont work ?

          – Anu Viswan
          Dec 31 '18 at 9:06





          @bradbury9 am curious on why above won't work. I have added sample client code now, with output. Why do you think above wont work ?

          – Anu Viswan
          Dec 31 '18 at 9:06




          1




          1





          Sorry, misread the question

          – bradbury9
          Dec 31 '18 at 9:07





          Sorry, misread the question

          – bradbury9
          Dec 31 '18 at 9:07













          This works .I didn't know this is as simple as that.So the problem is that the constraint in both the places(calling method and the method it calls) should be same.Thanks to both of you for looking into this

          – rahul
          Dec 31 '18 at 9:35





          This works .I didn't know this is as simple as that.So the problem is that the constraint in both the places(calling method and the method it calls) should be same.Thanks to both of you for looking into this

          – rahul
          Dec 31 '18 at 9:35


















          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%2f53985246%2fhow-to-call-generic-property-from-another-generic-method%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas