364,033 Members | 4781 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Infer SqlDbType Enumeration Member via Reflection

Brett  Kelly
P: n/a
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

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


Jon Skeet [C# MVP]
P: n/a
Jon Skeet [C# MVP]
Brett Kelly <inkedmn@gmail.com> wrote:[color=blue]
> At this point, I'm just trying to get reflection to work... This is
> what I've got so far:[/color]

<snip>
[color=blue]
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

Martin Robins
P: n/a
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.


"Brett Kelly" <inkedmn@gmail.com> wrote in message
news:1118969961.744461.179370@f14g2000cwb.googlegr oups.com...[color=blue]
> 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
>[/color]


Nov 17 '05 #3

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

Thanks!

Martin Robins wrote:[color=blue]
> 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" <inkedmn@gmail.com> wrote in message
> news:1118969961.744461.179370@f14g2000cwb.googlegr oups.com...[color=green]
> > 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
> >[/color][/color]

Nov 17 '05 #4

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp