424,303 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

Infer SqlDbType Enumeration Member via Reflection

P: n/a
Hello all,

I'm in a situation where I need to retrieve a member from the
System.Data.SqlDbType enumeration knowing only the type name.

At this point, I'm just trying to get reflection to work... This is
what I've got so far:

using System;
using System.Reflection;
using System.Data;

namespace PleaseFreakinWork
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("System.Data.SqlDbType");
Console.WriteLine("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}

And when I run this, it blows up on the Console.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (for example), that
doesn't work.

I've tried loading the System.Data.dll assembly, but it errors out
saying that the file or one of its dependencies could not be found...

Help!

TIA,

Brett

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Brett Kelly <in*****@gmail.com> wrote:
At this point, I'm just trying to get reflection to work... This is
what I've got so far:
<snip>
And when I run this, it blows up on the Console.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (for example), that
doesn't work.


Yup. That's because System.Reflection.Assembly is in mscorlib, but
SqlConnection isn't. If you don't provide an assembly name in the type
name, only mscorlib and the currently executing assembly are searched.

To find what to use for a particular type, use typeof(...) in a test
program and write out the AssemblyQualifiedName property. For example,
for SqlConnection it's:

System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
I could be off base here; but am I reading correctly that you want to be
able to retrieve the enumeration value based on the name of the member, ie
given the string "VarChar", you want to set a value to SqlDbType.Varchar?

If this is the case; SqlDbType value =
(SqlDbType)Enum.Parse(typeof(SqlDbType), "VarChar", true); will achieve it.
If not, please ignore me.
"Brett Kelly" <in*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello all,

I'm in a situation where I need to retrieve a member from the
System.Data.SqlDbType enumeration knowing only the type name.

At this point, I'm just trying to get reflection to work... This is
what I've got so far:

using System;
using System.Reflection;
using System.Data;

namespace PleaseFreakinWork
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("System.Data.SqlDbType");
Console.WriteLine("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}

And when I run this, it blows up on the Console.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (for example), that
doesn't work.

I've tried loading the System.Data.dll assembly, but it errors out
saying that the file or one of its dependencies could not be found...

Help!

TIA,

Brett

Nov 17 '05 #3

P: n/a
Sir, that is *precisely* what I was after :)

Thanks!

Martin Robins wrote:
I could be off base here; but am I reading correctly that you want to be
able to retrieve the enumeration value based on the name of the member, ie
given the string "VarChar", you want to set a value to SqlDbType.Varchar?

If this is the case; SqlDbType value =
(SqlDbType)Enum.Parse(typeof(SqlDbType), "VarChar", true); will achieve it.
If not, please ignore me.
"Brett Kelly" <in*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello all,

I'm in a situation where I need to retrieve a member from the
System.Data.SqlDbType enumeration knowing only the type name.

At this point, I'm just trying to get reflection to work... This is
what I've got so far:

using System;
using System.Reflection;
using System.Data;

namespace PleaseFreakinWork
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("System.Data.SqlDbType");
Console.WriteLine("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}

And when I run this, it blows up on the Console.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (for example), that
doesn't work.

I've tried loading the System.Data.dll assembly, but it errors out
saying that the file or one of its dependencies could not be found...

Help!

TIA,

Brett


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.