simple typedef equivalent in C#

G

Greg

I can see there is no simple way to define a type like just a simple
C++ typedef,
such as

typdef signaltype double;

Using #define seems like a bad idea if it works atall. Using a struct
looks promising but then is there a way of referncing a value without
having to use the dot operator.
 
B

Ben Voigt

Greg said:
I can see there is no simple way to define a type like just a simple
C++ typedef,
such as

typdef signaltype double;

Using #define seems like a bad idea if it works atall. Using a struct
looks promising but then is there a way of referncing a value without
having to use the dot operator.

There's

using signaltype = double;

But that just creates a new alias so: (1) the name isn't available in other
files, and (2) it's the same type, as far as typeof and function overloading
are concerned.

So I've made a compiler that uses Reflection.Emit to create new value types
that automatically cast to/from ordinary types and allow methods to be added
aspect-style (such as ToString, TryParse).
 
G

Greg

Ben said:
There's

using signaltype = double;

But that just creates a new alias so: (1) the name isn't available in other
files, and (2) it's the same type, as far as typeof and function overloading
are concerned.

Alias is perfect because that way I can distinguish between the defined
type and when a double is used for something else. Could it be put in
a namespace ?
So I've made a compiler that uses Reflection.Emit to create new value types
that automatically cast to/from ordinary types and allow methods to be added
aspect-style (such as ToString, TryParse).

OK, it is good that someone into compilers has common sense, how does
that work.
Is this variation / extension available online ?
 
B

Ben Voigt

Greg said:
Alias is perfect because that way I can distinguish between the defined
type and when a double is used for something else. Could it be put in
a namespace ?

What I was trying to make clear was that you can't distinguish. It's
another name for the same thing. Only the source code will have the
distinction. You can put it in a namespace, but only at the very top (wierd
rule) and the only effect is that it goes away at the closing brace of the
namespace instead of the end of the file. There's no way to export the
alias to other files either.
OK, it is good that someone into compilers has common sense, how does
I wouldn't say I'm into compilers. Just had a bunch of types defined in a
non-.NET language (ASN.1 subset actually) and wanted to use them. It's a
two-step process, ASN.1 parsing in perl which writes out an xml file, and
the Reflection/Emit part which an assembly as described by the xml file.
that work.
Is this variation / extension available online ?
No, not presently. The source for it is owned by my employer or possibly
the company we're under contract to. I may eventually try to make a freely
available web service where you can submit your xml and receive the compiled
dll, but I doubt the source will be released. It's rather specialized at
present anyway, a lot of changes would be needed before it would become
generally useful.

Here's an example typedef, decompiled by .NET Reflector

ASN.1:
--
-- 32 bit signed integer
--
INT-I32 ::= INTEGER (-2147483648..2147483647)


xml:
<typedef>
<name>INT-I32</name>
<type>
<base>INTEGER</base>
<range>
<min>-2147483648</min>
<max>2147483647</max>
</range>
</type>
<comment>32 bit signed integer</comment>
</typedef>


..NET Reflector's C# view:
[StructLayout(LayoutKind.Sequential), ValueType("INT-I32", Comment="32 bit
signed integer", ConstantType=typeof(int)),
DebuggerDisplay("{DebugDisplay}")]
public struct IntI32 : IDebugDisplay, IComparable, IFormattable,
IConvertible, IComparable<int>, IComparable<IntI32>, IEquatable<int>,
IEquatable<IntI32>, ICloneable, IEncodable, IMderEncodable
{
public const int MinValue = -2147483648;
public const int MaxValue = 0x7fffffff;
private readonly int internalValue;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public override string DebugDisplay { get; }
public IntI32([In] int value);
public static implicit operator IntI32([In] int value);
public IntI32([In] IntI32 original);
public static implicit operator int([In] IntI32 castFrom);
public sealed override int CompareTo(object obj);
public sealed override string ToString(string format, IFormatProvider
formatProvider);
public sealed override TypeCode GetTypeCode();
bool IConvertible.ToBoolean(IFormatProvider provider);
char IConvertible.ToChar(IFormatProvider provider);
sbyte IConvertible.ToSByte(IFormatProvider provider);
byte IConvertible.ToByte(IFormatProvider provider);
short IConvertible.ToInt16(IFormatProvider provider);
ushort IConvertible.ToUInt16(IFormatProvider provider);
int IConvertible.ToInt32(IFormatProvider provider);
uint IConvertible.ToUInt32(IFormatProvider provider);
long IConvertible.ToInt64(IFormatProvider provider);
ulong IConvertible.ToUInt64(IFormatProvider provider);
float IConvertible.ToSingle(IFormatProvider provider);
double IConvertible.ToDouble(IFormatProvider provider);
decimal IConvertible.ToDecimal(IFormatProvider provider);
DateTime IConvertible.ToDateTime(IFormatProvider provider);
public sealed override string ToString(IFormatProvider provider);
object IConvertible.ToType(Type conversionType, IFormatProvider
provider);
public sealed override int CompareTo(int other);
public override int CompareTo(IntI32 other);
public sealed override bool Equals(int other);
public override bool Equals(IntI32 other);
public override object Clone();
public override string ToString();
public override void Into(IEncoder buffer);
public override void Into(MderEncodingBuffer buffer);
public string ToString(string format);
public override bool Equals(object obj);
public override int GetHashCode();
public override string LTM.IDebugDisplay.DebugDisplay { get; }
}
 
G

Greg

Ben said:
What I was trying to make clear was that you can't distinguish. It's
another name for the same thing.

Reflection (emit) used to be called compiling, I suppose it would have
more accurately been called real time compilation. I have a problem
with people generating opcodes from their program trees and then
claiming it is faster than an optimisation / data structure.
distinction. You can put it in a namespace, but only at the very top (wierd
rule) and the only effect is that it goes away at the closing brace of the
namespace instead of the end of the file. There's no way to export the
alias to other files either.

I wouldn't say I'm into compilers. Just had a bunch of types defined in a
non-.NET language (ASN.1 subset actually) and wanted to use them. It's a
two-step process, ASN.1 parsing in perl which writes out an xml file, and
the Reflection/Emit part which an assembly as described by the xml file.

No, not presently. The source for it is owned by my employer or possibly
the company we're under contract to. I may eventually try to make a freely
available web service where you can submit your xml and receive the compiled
dll, but I doubt the source will be released. It's rather specialized at
present anyway, a lot of changes would be needed before it would become
generally useful.

Here's an example typedef, decompiled by .NET Reflector

ASN.1:
--
-- 32 bit signed integer
--
INT-I32 ::= INTEGER (-2147483648..2147483647)


xml:
<typedef>
<name>INT-I32</name>
<type>
<base>INTEGER</base>
<range>
<min>-2147483648</min>
<max>2147483647</max>
</range>
</type>
<comment>32 bit signed integer</comment>
</typedef>

That is alot of text just to define a range. Why the comment everyone
knows what an integer is, don't they ?
.NET Reflector's C# view:
[StructLayout(LayoutKind.Sequential), ValueType("INT-I32", Comment="32 bit
signed integer", ConstantType=typeof(int)),
DebuggerDisplay("{DebugDisplay}")]
public struct IntI32 : IDebugDisplay, IComparable, IFormattable,
IConvertible, IComparable<int>, IComparable<IntI32>, IEquatable<int>,
IEquatable<IntI32>, ICloneable, IEncodable, IMderEncodable
{
public const int MinValue = -2147483648;
public const int MaxValue = 0x7fffffff;
private readonly int internalValue;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public override string DebugDisplay { get; }
public IntI32([In] int value);
public static implicit operator IntI32([In] int value);
public IntI32([In] IntI32 original);
public static implicit operator int([In] IntI32 castFrom);
public sealed override int CompareTo(object obj);
public sealed override string ToString(string format, IFormatProvider
formatProvider);
public sealed override TypeCode GetTypeCode();
bool IConvertible.ToBoolean(IFormatProvider provider);
char IConvertible.ToChar(IFormatProvider provider);
sbyte IConvertible.ToSByte(IFormatProvider provider);
byte IConvertible.ToByte(IFormatProvider provider);
short IConvertible.ToInt16(IFormatProvider provider);
ushort IConvertible.ToUInt16(IFormatProvider provider);
int IConvertible.ToInt32(IFormatProvider provider);
uint IConvertible.ToUInt32(IFormatProvider provider);
long IConvertible.ToInt64(IFormatProvider provider);
ulong IConvertible.ToUInt64(IFormatProvider provider);
float IConvertible.ToSingle(IFormatProvider provider);
double IConvertible.ToDouble(IFormatProvider provider);
decimal IConvertible.ToDecimal(IFormatProvider provider);
DateTime IConvertible.ToDateTime(IFormatProvider provider);
public sealed override string ToString(IFormatProvider provider);
object IConvertible.ToType(Type conversionType, IFormatProvider
provider);
public sealed override int CompareTo(int other);
public override int CompareTo(IntI32 other);
public sealed override bool Equals(int other);
public override bool Equals(IntI32 other);
public override object Clone();
public override string ToString();
public override void Into(IEncoder buffer);
public override void Into(MderEncodingBuffer buffer);
public string ToString(string format);
public override bool Equals(object obj);
public override int GetHashCode();
public override string LTM.IDebugDisplay.DebugDisplay { get; }
}

Why do you need all that conversion ? I don't know what the hash code
is for, or the Into function. Why are you overriding equals ? There
is no point in creating a range for this type of integer ? You don't
need another string conversion function. I don't think this is good IT.
 

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