Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7852831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:30:57+00:00 2026-06-02T19:30:57+00:00

public class TestClass1 { public void TestMethod1() { new Logger<TestClass1>().Log(LogType.Info, FROM TESTCLASS1.TESTMETHOD1); } }

  • 0
public class TestClass1
{
    public void TestMethod1()
    {
        new Logger<TestClass1>().Log(LogType.Info, "FROM TESTCLASS1.TESTMETHOD1");
    }
}

When instantiating Logger and passing in TestClass1 as generic T, there could be a problem if the developer types in some thing else as opposed to TestClass1. For example, following line of code could be an issue.

new Logger<Test123>().Log(LogType.Info, "FROM TESTCLASS1.TESTMETHOD1");

How can I write the following line of code that it would automatically infer TestClass1 and pass it?

new Logger<TestClass1>().Log(LogType.Info, "FROM TESTCLASS1.TESTMETHOD1");

I have tried the following 2 lines of code but no luck.

new Logger<this>().Log(LogType.Info, "FROM TESTCLASS1.TESTMETHOD1");
new Logger<this.GetType>().Log(LogType.Info, "FROM TESTCLASS1.TESTMETHOD1");

UPDATE
Through out my application, I could be passing in any class whats so ever such as TestClass1, MyClass123 etc. etc. so I may not be able to put any generic constraints.

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:30:58+00:00Added an answer on June 2, 2026 at 7:30 pm

    How about an extension method:

    public static Logger<T> GetLogger<T>(this T obj)
    {
        return new Logger<T>();
    }
    

    so you can

    this.GetLogger()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: public class TestClass { public static void main(String[] args) { TestClass t =
I have two classes: public class TestClass1 { public int TestInt { get; set;
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
I have this type of class hierarchy: public interface IA{} public interface IB{void Foo();}
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
[TestClass] public class FooTests { [TestMethod] public void TestFoo() { var fooMock = MockRepository.GenerateMock<IFoo>();
@Entity public class TestClass implements Serializable{ private Integer id; private Set<String> mySet; @Id @GeneratedValue
My jpa looks like below public class TESTClass implements Serializable { ... private String
Given I have a class with two constructors: public class TestClass { ObjectOne o1;
This is my test [TestClass] public class RepositoryTests { private APurchaseOrderRepository _repository; [TestInitialize] public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.