samedi 20 juin 2020

how to get the class name at run time from a generic method that is in another class?

I am trying to generate my baseurl using UriBuilder. I have created a generic "GetRequestUrl" which is in my TestUtil class. How can I get the name of my Test class at run time using this method and append to the string serviceAPI

//Here is the GetRequestUrl method in my TestUtil class

public class TestUtil
{
        public string GetRequestUrl(string serviceName)
        {
            string serviceAPI = this.GetType().BaseType.Name;
            var requestUrl = new UriBuilder();
            requestUrl.Scheme = "http";
            requestUrl.Host = "svc-" + serviceName + "." + 
       ConfigurationManager.AppSettings["TestEnvironment"] + "-example.com/api/";
            requestUrl.Path = serviceAPI;
            Uri uri = requestUrl.Uri;

            return uri.ToString();

        }
}

//Here is my Test class where I want the Class name "TestClass" to append to serviceAPI string at run time, but I am getting TestUtil. I have tried following..

this.GetType().Name;

this.GetType().BaseType.Name;

MethodBase.GetCurrentMethod().DeclaringType.Name;

public class TestClass
{
TestUtil util = new TestUtil();
    [Test]
    public void Method1()
{
     string fullUrl = util.GetRequestUrl("APIServiceName");

}

}




Aucun commentaire:

Enregistrer un commentaire