Late-binding: (Un)boxing an Object type to a primitive and othertypes

P

puzzlecracker

I have a class that contains Append-like members for each primitive
type, string and INetSerializable:

public class TypeAppender
{
public void Append(int i ) {}
public void Append(double d) {}
public void Append(string s){}i){}
public void Append(INetSerializable ins){}
}

From a different class, I want to call this method "generically so to
speak", by passing Object

say I have something like that:

class SomeClientClass
{
TypeAppender _appender=new TypeAppender ();
Dictionary<string, Object> _cmdTable =new Dictionary<string,
Object>();

public void Process()
{
foreach(KeyValuePair<string, Object> pair in cmdTable )
{
_appender.Append(pair.Key);

Object obj = pair.Value;
if (obj is int)
_appender..Append((int)obj);
else if (obj is double)
_appender..Append((double)obj);
else if (obj is char)
_appender..Append((char)obj);
else if (obj is string)
_appender..Append((string)obj);
}
}
public void AddParam<T>(string key, T value)
{
_cmdTable.Add(key, value);
}
}

Question #1: Will pair.Value be unboxed to a correct primitive?

Question #2: Any problesm with AddParam<T> member function?


Thanks
 
P

Pavel Minaev

I have a class that contains Append-like members for each primitive
type, string and INetSerializable:

       public class TypeAppender
       {
             public void Append(int i ) {}
             public void Append(double d) {}
             public void Append(string s){}i){}
             public void Append(INetSerializable ins){}
        }

From a different class, I want to call this method "generically so to
speak", by passing Object

say  I have something like that:

    class SomeClientClass
    {
        TypeAppender _appender=new TypeAppender ();
        Dictionary<string, Object> _cmdTable =new Dictionary<string,
Object>();

        public void Process()
        {
            foreach(KeyValuePair<string, Object> pair in cmdTable )
            {
                  _appender.Append(pair.Key);

                  Object obj = pair.Value;
                  if (obj is int)
                     _appender..Append((int)obj);
                  else if (obj is double)
                     _appender..Append((double)obj);
                  else if (obj is char)
                     _appender..Append((char)obj);
                  else if (obj is string)
                     _appender..Append((string)obj);
            }
        }
        public void AddParam<T>(string key, T value)
        {
                _cmdTable.Add(key, value);
        }
    }

Question #1:  Will pair.Value be unboxed to a correct primitive?

Not sure what you mean by "correct primitive". Naturally, if you do an
"is T" check immediately followed by "(T)" cast, this will unbox the
value to T if and only if it was a boxed T.

Question #2:  Any problesm with  AddParam<T> member function?

I don't see any. There is an implicit cast to Object for any T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top