CLR and accessible features

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I was reading about MSIL and i came to know that there are some features of
CLR that can be only accessed by programming in IL. I want to know what are
these features which cannot be accessed by languages like c# or vb.net

Thanks
pradeep_tp
 
| Hi all,
|
| I was reading about MSIL and i came to know that there are some features
of
| CLR that can be only accessed by programming in IL. I want to know what
are
| these features which cannot be accessed by languages like c# or vb.net
|
| Thanks
| pradeep_tp

What exactly do you mean with "features of the CLR", MSIL allows you to do
things which aren't possible/allowed to do in higher level languages, but as
far as the CLR goes there is nothing that IL does what can't be done in C#
for instance.

Willy.
 
Hi willy

By saying "features of CLR" , I mean CLR's facilties. As you have
mentioned, what are those things which are allowed/possisble only through
MSIL.

thanks
pradeep_tp
 
One thing that can be done using IL but cannot be done using C# or
VB.NET is that the access modifier protected internal. The protected
internal in C# or VB.NET is an "OR" operator that is it is either
protected or internal. In IL it possible to declare "protected or
internal" and "protected and internal".
 
Oh, I see, but I'm afraid I can't answer this question because I don't know
all languages that target the CLR.
If I restrict myself to C#, VB.NET and managed C++, all I can come up with
is:
- managed exports, that is the possibility to expose managed functions to
unmanaged callers.
- overloads of methods with covariant return types

Willy.

| Hi willy
|
| By saying "features of CLR" , I mean CLR's facilties. As you have
| mentioned, what are those things which are allowed/possisble only through
| MSIL.
|
| thanks
| pradeep_tp
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > | > | Hi all,
| > |
| > | I was reading about MSIL and i came to know that there are some
features
| > of
| > | CLR that can be only accessed by programming in IL. I want to know
what
| > are
| > | these features which cannot be accessed by languages like c# or vb.net
| > |
| > | Thanks
| > | pradeep_tp
| >
| > What exactly do you mean with "features of the CLR", MSIL allows you to
do
| > things which aren't possible/allowed to do in higher level languages,
but as
| > far as the CLR goes there is nothing that IL does what can't be done in
C#
| > for instance.
| >
| > Willy.
| >
| >
| >
 
| One thing that can be done using IL but cannot be done using C# or
| VB.NET is that the access modifier protected internal. The protected
| internal in C# or VB.NET is an "OR" operator that is it is either
| protected or internal. In IL it possible to declare "protected or
| internal" and "protected and internal".
|

However, it can be done in managed C++.

Willy.
 
Yes, I know it can be done in Managed C++. But the intial question was
what can be done in IL that cannot be done in C# or VB.NET.
 
Willy Denoyette said:
Oh, I see, but I'm afraid I can't answer this question because I don't know
all languages that target the CLR.
If I restrict myself to C#, VB.NET and managed C++, all I can come up with
is:
- managed exports, that is the possibility to expose managed functions to
unmanaged callers.
- overloads of methods with covariant return types

Do any of the languages allow you to modify a boxed value in place? For
instance, if I had a struct:

public struct Foo
{
public int x;
}

and put that into an ArrayList, in C# I certainly wouldn't be able to
modify the value in the list - I'd have to overwrite it with a new
boxed value after unboxing and modifying the other one, if you see what
I mean.

My guess is that it's available in C++, but it doesn't hurt to know
these things...
 
| Yes, I know it can be done in Managed C++. But the intial question was
| what can be done in IL that cannot be done in C# or VB.NET.
|

Ok, some nitpickng maybe, but the OP said "...by languages like C# and
VB.NET....", which IMO has a broader scope than "in C# and VB.NET".
But I was expecting this when I replied, that's why I said in my other reply
that it's hard to tell what can be done in IL that can't be done in higher
level languages.
The point is, if you realy need something that can't be done in your
language of choice, are you going to do it in IL when you can do it in
another higher level language? Not an easy question to answer either.


Willy.
 
| > Oh, I see, but I'm afraid I can't answer this question because I don't
know
| > all languages that target the CLR.
| > If I restrict myself to C#, VB.NET and managed C++, all I can come up
with
| > is:
| > - managed exports, that is the possibility to expose managed functions
to
| > unmanaged callers.
| > - overloads of methods with covariant return types
|
| Do any of the languages allow you to modify a boxed value in place? For
| instance, if I had a struct:
|
| public struct Foo
| {
| public int x;
| }
|
| and put that into an ArrayList, in C# I certainly wouldn't be able to
| modify the value in the list - I'd have to overwrite it with a new
| boxed value after unboxing and modifying the other one, if you see what
| I mean.
|
| My guess is that it's available in C++, but it doesn't hurt to know
| these things...
|

Don't know about other languages, but AFAIK the managed C++ language has no
way to do it either and the compiler won't generate code to modify
"in-place" (AFIAK), at least not in a 'safe' context.
In an unsafe context, I guess there is, but the question is; if it's more
efficient than an unbox/modify/box trip?

Willy.
 
Willy Denoyette said:
Don't know about other languages, but AFAIK the managed C++ language has no
way to do it either and the compiler won't generate code to modify
"in-place" (AFIAK), at least not in a 'safe' context.
In an unsafe context, I guess there is, but the question is; if it's more
efficient than an unbox/modify/box trip?

I seem to remember testing it in a sample program by modifying some IL
generated from C# - no need for unsafe code, it's just an IL unbox
without a copy. I think it made a fairly significant difference, but
only in a situation which is limited by this kind of operation
happening a lot. Of course, generics solve this problem in a rather
neater way anyway, if you're able to use 2.0 :)
 
| > Don't know about other languages, but AFAIK the managed C++ language has
no
| > way to do it either and the compiler won't generate code to modify
| > "in-place" (AFIAK), at least not in a 'safe' context.
| > In an unsafe context, I guess there is, but the question is; if it's
more
| > efficient than an unbox/modify/box trip?
|
| I seem to remember testing it in a sample program by modifying some IL
| generated from C# - no need for unsafe code, it's just an IL unbox
| without a copy. I think it made a fairly significant difference, but
| only in a situation which is limited by this kind of operation
| happening a lot. Of course, generics solve this problem in a rather
| neater way anyway, if you're able to use 2.0 :)
|

Jon,
Sorry, I was only thinking about higher level languages, I'm sure it can be
done in IL, however, I'm not sure it passes the verifier, remember (tweaked)
IL is per definition 'unsafe' (well non-verifiable), Peverify is your friend
here :-).

Willy.
 
Do any of the languages allow you to modify a boxed value in place?

This works for me:

// Old syntax
__value struct Foo
{
public:
Foo(int x) { this->x = x; }
virtual String * ToString() { return __box(x)->ToString(); }
int x;
};

int main()
{
Object *o = __box(Foo(42));
dynamic_cast<Foo*>(o)->x = 123;
Console::WriteLine(o);
return 0;
}

// New syntax, compiles with /clr:safe
value struct Foo
{
public:
Foo(int x) { this->x = x; }
virtual String^ ToString() override { return x.ToString(); }
int x;
};

int main()
{
Object ^o = Foo(42);
dynamic_cast<Foo^>(o)->x = 123;
Console::WriteLine(o);
return 0;
}


To add to the list of things you can't do in C#, VB or C++, I don't
think any of them ever generates the tail. or cpblk IL instructions.


Mattias
 
Hi all,

Mattias has given some interesting information. I will now find more about
tail and cpblk.

I thank you all for your help. I hope i get to hear more about this from
more experts. I also would like to express my special thanks to john skeet. I
have read many of his blogs and find it all very interesting. I feel honoured
to see your reply on this thread john :).

thanks
pradeep
 
But, this doesn't modify the boxed value in place, it unboxes before
modifying.

Take a look at the IL....
....
IL_0013: isinst Foo
IL_0018: unbox Foo
IL_001d: ldc.i4.s 123
IL_001f: stfld int32 Foo::x
....

Willy.


| >Do any of the languages allow you to modify a boxed value in place?
|
| This works for me:
|
| // Old syntax
| __value struct Foo
| {
| public:
| Foo(int x) { this->x = x; }
| virtual String * ToString() { return __box(x)->ToString(); }
| int x;
| };
|
| int main()
| {
| Object *o = __box(Foo(42));
| dynamic_cast<Foo*>(o)->x = 123;
| Console::WriteLine(o);
| return 0;
| }
|
| // New syntax, compiles with /clr:safe
| value struct Foo
| {
| public:
| Foo(int x) { this->x = x; }
| virtual String^ ToString() override { return x.ToString(); }
| int x;
| };
|
| int main()
| {
| Object ^o = Foo(42);
| dynamic_cast<Foo^>(o)->x = 123;
| Console::WriteLine(o);
| return 0;
| }
|
|
| To add to the list of things you can't do in C#, VB or C++, I don't
| think any of them ever generates the tail. or cpblk IL instructions.
|
|
| Mattias
|
| --
| Mattias Sjögren [C# MVP] mattias @ mvps.org
| http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| Please reply only to the newsgroup.
 
Willy,
it unboxes before modifying.

Right, but to quote the spec for the unbox instruction:

"Unlike box, which is required to make a copy of a value type for use
in the object, unbox is not required to copy the value type from the
object. Typically it simply computes the address of the value type
that is already present inside of the boxed object."


If a copy that was modified, Console::WriteLine(o) would print the
original value 42.


Mattias
 
| Willy,
|
| >it unboxes before modifying.
|
| Right, but to quote the spec for the unbox instruction:
|
| "Unlike box, which is required to make a copy of a value type for use
| in the object, unbox is not required to copy the value type from the
| object. Typically it simply computes the address of the value type
| that is already present inside of the boxed object."
|
|
| If a copy that was modified, Console::WriteLine(o) would print the
| original value 42.
|
|
| Mattias
|
| --
| Mattias Sjögren [C# MVP] mattias @ mvps.org
| http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| Please reply only to the newsgroup.

You are right of course (wasn't awake I guess when I replied), Unbox returns
a managed pointer the home of Foo::x) and the 'stfld' stores the value into
the home of x. This is exactly what C# doesn't allow. Therefore, the
following C# equivalent won't compile...

object o = new Foo(42);
((Foo)o).x = 123;

Willy.
 
pradeep_TP said:
Hi all,

I was reading about MSIL and i came to know that there are some
features of CLR that can be only accessed by programming in IL. I
want to know what are these features which cannot be accessed by
languages like c# or vb.net

One feature is that all the high level languages I know about insist
that array indexes are 0-based, the CLI allows you to specify the range
of the index (for a multi-dimensional array), and so it does not have to
be 0-based.

Richard
 
Back
Top