Infer SqlDbType Enumeration Member via Reflection

B

Brett Kelly

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
 
J

Jon Skeet [C# MVP]

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

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
 
M

Martin Robins

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.
 
B

Brett Kelly

Sir, that is *precisely* what I was after :)

Thanks!

Martin said:
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 said:
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
 

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