473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

simple typedef equivalent in C#

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.

Jan 10 '07 #1
5 5708

"Greg" <im*****@hotmail.co.ukwrote in message
news:11**********************@i56g2000hsf.googlegr oups.com...
>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).
Jan 10 '07 #2

Ben Voigt wrote:
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 ?

Jan 10 '07 #3

Greg wrote:
Ben Voigt wrote:
<snipReflection.Emit
lol, reflection is pointless, I don't see why anyone would need this it
is insane ?

Jan 10 '07 #4

"Greg" <im*****@hotmail.co.ukwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...
>
Ben Voigt wrote:
>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 ?
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.
>
>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
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; }
}
Jan 11 '07 #5

Ben Voigt wrote:
"Greg" <im*****@hotmail.co.ukwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...


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

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

Jan 11 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
2
by: Mehta Shailendrakumar | last post by:
Hi all, Have alook at two different declarations below: typedef unsigned char uc; typedef void (*fptr) (void); The first declaration has a left part which is a C keyword: "unsigned char"...
7
by: Dennis Myrén | last post by:
Hi. Is there any way to define an alias for a type like you could in C++: typedef int NUMBER; I tried using #define but that did not work. Thank you. -- Regards,
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
10
by: Christian Christmann | last post by:
Hi, what is the difference between a "#define" and typedef? A "#define" is handled by the preprocessor and serves as a name substitude whereas "typedef" is a declaration of a new data type...
4
by: George | last post by:
Is there a C++ typedef equivalent in C#? Thanks, -- George
4
by: rajm2019 | last post by:
hi to all i am new to c++ i want to know wat do we mean bye typedef support::PtrObject<DirectoryDirectoryRef when we are using (DirectoryRef abc); is abc an object of template class...
8
by: Carmen Sei | last post by:
I found in C++ a type is typedef several times and alot of time, I need to dig further to see the original type. like LPDWORD - it's a strange type, but when i dig further, i see it's a (DWORD...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.