473,883 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Casting 'object' to "real/useful" Type

Thanks George, I really am grateful for attempts to be helpful, but this
really doesn't answer the question in my OP.

What I am looking for is an explanation of WHY things are this way (I was
not looking for a work-around).

Again, I am appreciative of the feedback. I will note, that even though I
can use Interfaces, the calling application and the dynamically loaded
assembly both need compile-time refrences to the assembly that contains the
interfaces (as you accurately pointed out). This does not answer the core
question I have about the need to have compile-time references.

Again, the question is all about an explanation of WHY we must have
compile-time references... and why Reflection cannot get us around that
apparent requrement.

-Robert


Jun 27 '08 #1
7 2432
Robert <A@B.comwrote :
Thanks George, I really am grateful for attempts to be helpful, but this
really doesn't answer the question in my OP.

What I am looking for is an explanation of WHY things are this way (I was
not looking for a work-around).

Again, I am appreciative of the feedback. I will note, that even though I
can use Interfaces, the calling application and the dynamically loaded
assembly both need compile-time refrences to the assembly that contains the
interfaces (as you accurately pointed out). This does not answer the core
question I have about the need to have compile-time references.

Again, the question is all about an explanation of WHY we must have
compile-time references... and why Reflection cannot get us around that
apparent requrement.
If you're willing to make *all* the calls via reflection, then you can
indeed work completely without any other references.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #2

You must have a compile time reference TO THE INTERFACE.
This is why the interface library should be stand alone...and you can put
the concrete's anywhere you'd like.

If you follow my example, it will show you ( I believe ) a non compile time
reference.

.....

as my example shows:

<rateQuoterObje ct enabled="true"
assemblyName="G ranadaCoder.App lications.Facto ryPatternExampl es.ConcreteObje ctsOutsideBaseA ssembly"
className="Gran adaCoder.Applic ations.FactoryP atternExamples. ConcreteObjects OutsideBaseAsse mbly.ConcreteRa teQuoters.UPSRa teQuoter"
ShippingCompany HomeState="NC"/>

I have a "Quoter" that isn't referenced. Or at least, doesn't need to be
referenced.

...
DotNet has to know ~at least~ about the interface if you want it to return
something other than object.

"Robert" <A@B.comwrote in message
news:Oo******** ******@TK2MSFTN GP05.phx.gbl...
Thanks George, I really am grateful for attempts to be helpful, but this
really doesn't answer the question in my OP.

What I am looking for is an explanation of WHY things are this way (I was
not looking for a work-around).

Again, I am appreciative of the feedback. I will note, that even though I
can use Interfaces, the calling application and the dynamically loaded
assembly both need compile-time refrences to the assembly that contains
the interfaces (as you accurately pointed out). This does not answer the
core question I have about the need to have compile-time references.

Again, the question is all about an explanation of WHY we must have
compile-time references... and why Reflection cannot get us around that
apparent requrement.

-Robert


Jun 27 '08 #3
On Apr 22, 2:26*pm, "Robert" <A...@B.comwrot e:
Thanks George, I really am grateful for attempts to be helpful, but this
really doesn't answer the question in my OP.

What I am looking for is an explanation of WHY things are this way (I was
not looking for a work-around).

Again, I am appreciative of the feedback. I will note, that even though I
can use Interfaces, the calling application and the dynamically loaded
assembly both need compile-time refrences to the assembly that contains the
interfaces (as you accurately pointed out). This does not answer the core
question I have about the need to have compile-time references.

Again, the question is all about an explanation of WHY we must have
compile-time references... and why Reflection cannot get us around that
apparent requrement.

-Robert
Hi,

You do not need add a refeence at compile time if you load all your
dlls using Assembly.Load() and call members using reflection.
It's a slow and convoluted way of doing it, but you can do it.

Jun 27 '08 #4
its because the .net runtime is strongly typed. to call a method or access a
property, the type of the object must be known at compile time. the major
workaround is as suggested using interfaces.

other languages (javascript, ruby, python, object-c, smalltalk, etc) support
dynamic binding, which allows your code to just call the method.

even java (which is also strongly typed) supports runtime re-compiles so
that plugins and remoting will work the way you expect. this feature was left
out of .net for various performance reasons.

..net now has a dlr (dynamic language runtime) that supports this behavior,
but you need to use a dlr language like IronPython, IronRuby or managed
JScript.
-- bruce (sqlwork.com)
"Robert" wrote:
Thanks George, I really am grateful for attempts to be helpful, but this
really doesn't answer the question in my OP.

What I am looking for is an explanation of WHY things are this way (I was
not looking for a work-around).

Again, I am appreciative of the feedback. I will note, that even though I
can use Interfaces, the calling application and the dynamically loaded
assembly both need compile-time refrences to the assembly that contains the
interfaces (as you accurately pointed out). This does not answer the core
question I have about the need to have compile-time references.

Again, the question is all about an explanation of WHY we must have
compile-time references... and why Reflection cannot get us around that
apparent requrement.

-Robert


Jun 27 '08 #5
Thank you Bruce for answering my specific question. I have a Quick followup
question:

Is it true then [given your response] that I _cannot_ do step 3 of the
following sequence from my C# application:

1. load an .NET assembly via Reflection - where my application has
absolutely no compile-time knowledge of the assembly or the types within
that assembly (i.e., no project reference to the assembly). This step, by
itself, is obviously easy to do using Reflection.

2. use Reflection to identify the types within that assembly. This step, by
itself, is obviously easy to do using Reflection.

3. instantiate a class of a specific type from within that assembly. This
step, given steps 1 and 2 above cannot happen, and for the reasons you
gave - yes?

For example, MyApp is a C# app that loads the Some3rdParty assembly, which
is a .NET assembly that contains a class named Frapper. Apparently because
the .NET runtime is strongly typed, MyApp _cannot_ create a Frapper instance
_unless_ MyApp also has a compile-time reference to the Some3rdParty
assembly. Is this correct, and for the reasons you stated?

I'm asking this because I want to verify that I understand your response.

Please note that I'm not asking about the merits of doing this, and I'm not
attempting to solve some problem "the hard way". My interest is mostly
academic... just wanting to understand things more clearly, and particularly
to find out if my understanding is incorrect. FWIW, I have already done a
couple of plug-in apps that go with the interface-based recommendations
others have made elsewhere in this thread. I'm clear on that... I just want
to know why, and I think I do now - provided that I understand your
response.

Thanks.

"bruce barker" <br*********@di scussions.micro soft.comwrote in message
news:3C******** *************** ***********@mic rosoft.com...
its because the .net runtime is strongly typed. to call a method or access
a
property, the type of the object must be known at compile time. the major
workaround is as suggested using interfaces.

other languages (javascript, ruby, python, object-c, smalltalk, etc)
support
dynamic binding, which allows your code to just call the method.

even java (which is also strongly typed) supports runtime re-compiles so
that plugins and remoting will work the way you expect. this feature was
left
out of .net for various performance reasons.

.net now has a dlr (dynamic language runtime) that supports this behavior,
but you need to use a dlr language like IronPython, IronRuby or managed
JScript.
-- bruce (sqlwork.com)
"Robert" wrote:
>Thanks George, I really am grateful for attempts to be helpful, but this
really doesn't answer the question in my OP.

What I am looking for is an explanation of WHY things are this way (I was
not looking for a work-around).

Again, I am appreciative of the feedback. I will note, that even though I
can use Interfaces, the calling application and the dynamically loaded
assembly both need compile-time refrences to the assembly that contains
the
interfaces (as you accurately pointed out). This does not answer the core
question I have about the need to have compile-time references.

Again, the question is all about an explanation of WHY we must have
compile-time references... and why Reflection cannot get us around that
apparent requrement.

-Robert




Jun 27 '08 #6
Robert,

On Tue, 22 Apr 2008 13:38:50 -0700, "Robert" <A@B.comwrote :
>Thank you Bruce for answering my specific question. I have a Quick followup
question:

Is it true then [given your response] that I _cannot_ do step 3 of the
following sequence from my C# application:

1. load an .NET assembly via Reflection - where my application has
absolutely no compile-time knowledge of the assembly or the types within
that assembly (i.e., no project reference to the assembly). This step, by
itself, is obviously easy to do using Reflection.

2. use Reflection to identify the types within that assembly. This step, by
itself, is obviously easy to do using Reflection.

3. instantiate a class of a specific type from within that assembly. This
step, given steps 1 and 2 above cannot happen, and for the reasons you
gave - yes?

For example, MyApp is a C# app that loads the Some3rdParty assembly, which
is a .NET assembly that contains a class named Frapper. Apparently because
the .NET runtime is strongly typed, MyApp _cannot_ create a Frapper instance
_unless_ MyApp also has a compile-time reference to the Some3rdParty
assembly. Is this correct, and for the reasons you stated?
No - consider this sample MyApp:

namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Assembly assembly =
Assembly.LoadFr om(@"c:\tmp\Som e3rdParty\bin\D ebug\Some3rdPar ty.dll");

Type frapperType = assembly.GetTyp es().Where(t =t.Name ==
"Frapper").Sing leOrDefault();

if(frapperType == null)
{
Console.WriteLi ne("Out of frappers");
return;
}

MethodInfo frappitMethod = frapperType.Get Methods().Where (m
=m.Name == "Frappit").Sing leOrDefault();
if(frappitMetho d == null)
{
Console.WriteLi ne("Frapper can't frap it");
return;
}

object frapperInstance =
Activator.Creat eInstance(frapp erType);
string result = (string)frappit Method.Invoke(f rapperInstance,
new object[0]);

Console.WriteLi ne(result);
}
}
}

it will attempt to load "Some3rdParty.d ll", find any class called
"Frapper", locate a method "Frappit" in that class (which hopefully
returns a string and doesn't have any parameters), and call it.

Here's Frapper

namespace Some3rdParty
{
public class Frapper
{
public string Frappit()
{
return "Greetings, Pr. Falken";
}
}
}

This works without any reference from the MyApp project to the
Some3rdParty project containing the Frapper class.

Regards,
Gilles.

Jun 27 '08 #7
<Snip>

Thank you Gilles. Apparently my understanding was incorrect. It really helps
to see and study the sample code that you so kindly provided.

-Robert

Jun 27 '08 #8

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

Similar topics

7
14858
by: Aryeh M. Friedman | last post by:
If have something like the following declartion: enum foo {One,Two,....,Ten}; How do I determine how many elements (enumerations)... in this case it is obviously 10 but I don't want to hard code that fact --Aryeh
11
2990
by: Nobody | last post by:
Heres the deal... I have an application where I have a list (as in a Windows list control, but thats not important) displayed to the user. I sort this list based on the list controls sort function (again, its not important that its Windows) which ends up calling a compare function in my code: int CompareFunc(char* str1, char* str2) { } this function returns -1, 0 or 1 which gets passed on to the internal quick
4
3141
by: Silas | last post by:
Hi, I use view to join difference table together for some function. However, when the "real" table fields changed (e.g. add/delete/change field). The view table still use the "old fields". Therefore everytimes when I change the real table, I also needed open the view table and save it by SQL enterprise manager manually for update the view table field.
2
1672
by: sandman | last post by:
I created an ImageList in the .NET Designer. Now I can't figure out where the images are located. Where is all this stuff stored and how do I read it? (me personally, I mean -the computer is doing it just fine.) sandman *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
30
2929
by: bblais | last post by:
Hello, Let me start by saying that I am coming from a background using Matlab (or Octave), and C++. I am going to outline the basic nuts-and-bolts of how I work in these languages, and ask for some help to find out how the same thing is done in Python. I am not sure what the standard is. In C++, I open up an editor in one window, a Unix shell in another. I write the code in the editor, then switch to the shell window for compile...
1
3137
by: Tyno Gendo | last post by:
Hi everyone I need to move on a step in my PHP... I know what classes are, both in PHP4 and 5 and I'm aware of "patterns" existing, but what I'm looking for are some real world projects eg. Open Source that people consider to use classes and patterns correctly. I lack a senior person to lead me in this so I feel I'm losing out on only using bare PHP class features and not really knowing how to design
3
1766
by: Mark Shroyer | last post by:
I guess this sort of falls under the "shameless plug" category, but here it is: Recently I used a custom metaclass in a Python program I've been working on, and I ended up doing a sort of write-up on it, as an example of what a "real life" __metaclass__ might do for those who may never have seen such a thing themselves. http://markshroyer.com/blog/2007/11/09/tilting-at-metaclass-windmills/ So what's the verdict? Incorrect? Missed the...
71
3398
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python files included in the Windows version of Python distribution. I'm not sure how much of the C-Python is implemented in C but I think the more modules implemented in C, the better performance and lower memory footprint it will get. I wonder if it's...
1
1604
by: jamesicus | last post by:
.......... just for curiosity? "real" XHTML served as content (MIME) type application/xhtml+xml will display in MSIE 6.0 release 2900 and 7.0 but they will not render xml content. However, MSIE 6.0 release 2800 and earlier displays a download screen -- selecting OPEN presents a plain, text only, rendered page -- sans style sheet. Here is an XHTML page served as content (MIME) type application/xhtml+xml for testing by those interested. ...
0
174
by: Ignacio Machin ( .NET/ C# MVP ) | last post by:
The difference between compile & runtime. CreateInstance works at runtime, you can pass ANY string to it (even an incorrect one like "123123123123") and it will compile Only at runtime you will get the error. And honestly, you HAVE to know something about your Class. otherwise, how do you know which method to call?
0
9798
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10864
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7980
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6008
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.