.NET and C#

  • Thread starter Thread starter komes
  • Start date Start date
K

komes

Hi everybody.
Is in .NET any class, which converts .NET data types to c# types? (i.e. gets
a variable of one of .NET data types and returns native c# type?).
Rgds,
Komes
 
komes said:
Is in .NET any class, which converts .NET data
types to c# types? (i.e. gets a variable of one of
.NET data types and returns native c# type?).

They are the same thing. When you declare a variable as "int" or
"float", it's the same as writing "Int32" or "Single".

Eq.
 
Maybe I'm having a "slow brain" day, but can you clarify what you mean?

C# doesn't really have any data types of its own (it merely has compile-time
aliases to the CLR types); the types are the same - i.e. typeof(int) ==
typeof(Int32).

Do you mean the names? i.e. give it "System.Int32" and it returns "int"? in
which case I am not aware of anything inbuilt, but the list of aliases isn't
long. You're throwing me a bit by talking about variables - is this for a
code generator?

Marc
 
OK, that's clear, but I have another problem in the same code. I want to
write some data with binarywriter from system.io. That method has 18
versions and takes object of different data types: bool, int, and so on. I
have a regular object, which has data of all types - read from database. I
want to make something like this:
Object o =...
binarywriterobject.write(o as o.getType())
But of course it's not possible, because it's like convertion to Type type.
Have you any idea how to solve this?
Rgds,
Komes
 
komes,

No, I don't think so. C# is just one of the languages and If there were such
a class or method or whatever for c# there must be for VB.NET, J# and
several other languages available. It is just not possible.

However as far as it goes for C# or VB.NET I believe you can use CodeDOM to
do the trick for you.

Try the following

[STAThread]
static void Main(string[] args)
{

CodeVariableDeclarationStatement var = new
CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Int32)),
"foo");
// Obtains an ICodeGenerator from a CodeDomProvider class.
CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeGenerator gen = provider.CreateGenerator();
// Generates source code using the code generator.
gen.GenerateCodeFromStatement(var, Console.Out, new
CodeGeneratorOptions());
}

this will generate source code for declaring variable *foo* of type int or
whatever Type obect provided. The good thing is that it will use C# keywords
for the primitive types. Thus the generated code will be:

int foo;

Having that string I believe you can extract the C# keyword using simple
string operations.
 
Binary writer is intended for writing /simple/ data (of known type) to
streams; it isn't clear what you are intending to write; if o is actually
just holding an int, string, etc (one of the ones that can already be
handled, but we don't know which at compile-time), then there is no easy way
to do this... perhaps the only way is via reflection with InvokeMember in
ExactBinding mode... not trivial, but possibly workable.

If you actually mean compund data (bespoke classes / structs), you have 2
choices:
1: write each field to the stream yourself (and read in the same order) -
essentially a compact form of manual serialization
or
2: look at BinaryFormatter().Serialize()

The latter will do a lot for you automatically, but is more verbose than the
former.

If that doesn't help, then post back...

Marc
 
Just write the object to the stream. While it is defined in your code as
"object," it is what it is. That is, while your variable is typed as
"object," the value of it will be a type. Example:

Object o = 25;

binarywriterobject.Write(o);

Because the value contained in the variable is an integer, the overload of
BinaryWriter.Write that takes an integer will be used.

Remember that all classes inherit object. Therefore, every instance of a
class *is* an object. However, it is still what it is. If you think of a
variable as a "box" it will help. The variable is not the value; it
*contains* the value. Yes, you refer to the value by referring to the
variable. But the variable itself is only a pointer to the actual instance.
It provides a "handle" that you can use to refer to the instance.

In mathematics (upon which all programming is based), algebra does the same
thing:

x + 5 = 10
x = 5

Note that the second equation says that "x" is equal to 5. Of course, "x" is
nothing but a placeholder, and provides a "handle" for working with an
as-yet-unknown number. So, when you refer to "x" you are not referring to
the variable itself, but to the number it represents.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.
 
BinaryWriter does not have a Write(object) overload, so this will not
compile. Sorry.

To force it to inspect the object to determine an appropriate overload to
innvoke, you need to use reflection:

//(where "writer" is the specific writer in question, and "obj" is the value
typed as object):
writer.GetType().InvokeMember("Write", BindingFlags.InvokeMethod |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.ExactBinding,
null, writer, new object[] { obj });

Marc
 
Kevin Spencer said:
Just write the object to the stream. While it is defined in your code as
"object," it is what it is. That is, while your variable is typed as
"object," the value of it will be a type. Example:

Object o = 25;

binarywriterobject.Write(o);

Because the value contained in the variable is an integer, the overload of
BinaryWriter.Write that takes an integer will be used.

I wrote a quick test just to see this in action (I'm curious even though
I've been developing VB.Net and C# for years now) ...

object a = null;
object b = 123;
object c = "123";

private void Show(object Value) { Console.WriteLine("object"); }
private void Show(int Value) { Console.WriteLine("int"); }
private void Show(string Value) { Console.WriteLine("string"); }

after running the above...I get the following output in the console window:

object
object
object


I don't mean to prove ya wrong Kev...just checking it out and found that you
were...the overloads that take an int and a string are never called...and
here is why...

When you create a variable of type object, and set it to a value type, the
variable does not hold the value. The variable is holding the "box"'ed
value. Since it's holding a box'ed value, it is seen as an object and not
the specified type (int or string). So, the first method (passing object
value) is called.

Correct me if I'm wrong, but that's how I see it happening here ;)

Mythran
 
BinaryWriter does not have a Write(object) overload, so this will not
compile. Sorry.

Hi Marc,

You're correct, of course. My mistake.

--

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

Marc Gravell said:
BinaryWriter does not have a Write(object) overload, so this will not
compile. Sorry.

To force it to inspect the object to determine an appropriate overload to
innvoke, you need to use reflection:

//(where "writer" is the specific writer in question, and "obj" is the
value typed as object):
writer.GetType().InvokeMember("Write", BindingFlags.InvokeMethod |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.ExactBinding,
null, writer, new object[] { obj });

Marc
 
Yes, sorry, Mythran. I had a brain fart, perhaps an early "senior moment."
;-)

Marc's advice is spot on.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.
 

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

Back
Top