Amit Lipman
Amit Lipman

Reputation: 715

c# how can I compare type of template type to builtin type on runtime

that code doesn't compile, anyone has any idea how to write this logic correctly?

public void FilBuff<T>(T p_tInput) 
    {
        if(typeid(p_tInput )== typeof(string))
        {
            m_bBuff = System.Text.Encoding.ASCII.GetBytes((string)p_tInput);
        }
    }

Upvotes: 0

Views: 65

Answers (1)

vcsjones
vcsjones

Reputation: 141638

Use typeof(T). Like this:

public void FilBuff<T>(T p_tInput) 
{
    if(typeof(T) == typeof(string))
    {
        m_bBuff = System.Text.Encoding.ASCII.GetBytes((string)p_tInput);
    }
}

As an aside, what you are doing with generics (not templates) is a little odd. It might be better to just use an overloaded method in your case.

Upvotes: 4

Related Questions