|
It's failing because your not loading the containing assembly correctly. If you read the documentation for Assembly.CreateInstance , it returns null if the type was not found. Since you're dealing with value types - which cannot be null - casting a null reference to a value type will throw an exception.
I think you're missing the point of the global assembly cache (GAC). You don't have to - nor should you - worry about where the assemblies are so long as they are in the GAC or the private path (the "bin" folder, for instance, in ASP.NET). .NET, first of all, does not use the %PATH% env var. If the Type you want to create is contained in an assembly in the GAC or the private path of your application, you simply create the Type and assembly - if not already loaded - is loaded from the GAC automatically (and if a pre-JIT'ed (native) assembly exists, it'll be used instead because it loads and executes faster).
Try this:
Type t = Type.GetType("System.Drawing.Size, System.Drawing", false, true);
Size s = (Size)t.Assembly.CreateInstance(t.ToString(), ...); Of course, there's many other ways to do this. The important thing is that you shouldn't worry about the paths to assemblies themselves unless they are your assembly in a non-private path, such as plugins or something. The .NET base class library (BCL) is in the GAC and should not be referenced by a local file path. Besides, the method you're using for finding the local path is extremely slow and inefficient. If nothing else, you should at least cache the directory once you've found it and avoid the large loop every time.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hello,
Thanks for you help. I've tried you code, but my app still does not work. If I try to get a System.Int32 or something else in the System assembly it works fine. But if I try to get a System.Drawing.Size it fails.
Type t1 = Type.GetType ("System.Reflection.Assembly", false, true);
Type t2 = Type.GetType ("System.Int32", false, true);
System.Drawing.Size s = new System.Drawing.Size (100, 200);
Type t3 = Type.GetType ("System.Drawing.Size, System.Drawing", true, true);
Type t4 = Type.GetType ("System.Drawing.Size", true, true);
What's my mistake ? I've included the link to the System.Drawing.dll in my project and I've also included a using System.Drawing; in my code. Is there any configuration to do for my app assembly ?
|
|
|
|
|
Stux wrote:
If I try to get a System.Int32 or something else in the System assembly it works fine.
Actually, those types are in the mscorlib.dll assembly, the implicit library that is always included.
Stux wrote:
What's my mistake ?
Good question. t3 is the correct code. Unless something is in the mscorlib.dll assembly (which is why I mentioned that above) or in the currently loaded assembly, you must give an assembly name like you did there. Everything looks right, though. Type.GetType will load the assembly in which the Type is contained if it isn't already. And you don't have to include a reference to it in your project if you plan on using Reflection. The BCL assemblies are in the GAC (at least they should be, open the folder \Windows\assembly and verify) which is alway checked after private paths (like the bin directory in ASP.NET) or assembly binding redirections in the .config file.
The only other thing I can think of that you could do is use the fully-qualified the type name, i.e. "System.Drawing.Size, System.Drawing, Culture=neutral, Version=1.0.3300.0, PublicKeyToken=b03f5f7f11d50a3a" (for .NET 1.0), although I've never had to do this on my work machine - but I only have one framework currently installed. Do you, by chance, have both .NET 1.0 and 1.1 installed?
Anyway, try using the FQN (fully-qualified name) and see if that works. Other than that, you're doing it right.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
You're right. I've both Frameworks installed, but I've already tested the fully-qualified type name. I've also tested the Project on an other machine, having only the .NET 1.1 Framework installed. Nothing worked
But I found an other solution :
Assembly a = Assembly.LoadWithPartialName ("System.Drawing");
Type t = a.GetType ("System.Drawing.Size");
I hope that will work until I've more time to examine the problem.
Thanks anyway.
Nice "Geek-Code-Block" I think I will also include one in my Profile.
|
|
|
|
|
Please help me ! I want to communicate bettwen a service and an application . But i don't know how to .
q
|
|
|
|
|
Use .NET Remoting.
Just search for "remoting" and/or "services" here on CP. Remember to look out for "obsolete" samples. (Ie. code written with a beta version of the framework.)
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
I want to experiement with COM+ application load balancing. What do I need to get started?
a. two machines with Win2000/2003 installed
b. What else do I need?
Would you recommend any article/references/book?
Thanks.
|
|
|
|
|
I am trying to load data to an string variable in the next way:
.
.
.
string s = "";
string text = "";
do {
If ( text have some conditions)
s = s + text + "\n";
text = reader.ReadLine( );
}
while (text != null);
.
.
Then I need to show string "s" to user using a multiline textbox created with Web Forms. The problem is that the previous loop lasts many, many minutes. It tries to load 2MB but in a slow manner, I imagine that the memory allocation is the problem. I need textbox for copy/paste purposes.
Is there anyway to enhance the speed of s = s + text + "\n"; ???
Best regards.
|
|
|
|
|
System.Text.StringBuilder is a little faster:
StringBuilder s = new StringBuilder();
string text = "";
do {
If ( text have some conditions)
s.AppendFormat("{1}\n", text);
text = reader.ReadLine( );
}
while (text != null);
|
|
|
|
|
Corinna,
Wouldn't this be even a little faster?
StringBuilder s = new StringBuilder();
string text;
while ((text = reader.readLine()) != null) {
if ( <condition> )
s.Append(text).Append('\n');
}
Regards,
Jeff Varszegi
[edit] Forgot to include the condition! I'm gonna test the impact of AppendFormat() versus the extra method call. JKV [/edit]
|
|
|
|
|
I was interested enough to run a performance test because I never used AppendFormat before, and I know that StringBuilder is highly optimized. I ran this code in Debug mode in VS .NET 2003 on my laptop:
StringBuilder sb;
string text = "abc";
int x, y;
long startTime, endTime;
<br>
startTime = DateTime.Now.Ticks;
for(y = 0; y < 5000; y++) {
sb = new StringBuilder(10000);
for (x = 0; x < 250; x++) {
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
sb.Append(text).Append('\n');
}
}
endTime = DateTime.Now.Ticks;
Console.WriteLine(((endTime - startTime) / 10000) + " ms using Append()");
<br>
startTime = DateTime.Now.Ticks;
for(y = 0; y < 5000; y++) {
sb = new StringBuilder(10000);
for (x = 0; x < 250; x++) {
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
sb.AppendFormat("{0}\n", text);
}
}
endTime = DateTime.Now.Ticks;
Console.WriteLine(((endTime - startTime) / 10000) + " ms using AppendFormat()");
It printed these results:
<font color="#000066">1682 ms using Append()</font>
<font color="#000066">7801 ms using AppendFormat()</font>
Interestingly enough, creating a separate variable to hold the format string and using that in calls to AppendFormat increased the run time of the second section a little. I still know next to nothing about the IL, but I'm sure there's some good explanation for this.
Regards,
Jeff Varszegi
|
|
|
|
|
I've create an inherit class of dataGrid and add it to form by press of a button. the class will dispose after sending a value to the form by dblclick. The problem is, I dont want to load the class everytime i press the button. I want it to work like load/unload when button press. Is there anyway to check control or class existence?
// this is how i add the class to form by button press
private void button2_Click(object sender, System.EventArgs e)
{
myLOV aLOV = new myLOV();
aForm.Controls.Add(aLOV);
}
PS: Working Environment is .NET Compact Framework on Windows CE 3.0
|
|
|
|
|
if (aLOV == null)
aLOV = new MyDataGrid();
if (aForm.Controls.Contains(aLOV))
aForm.Controls.Remove(aLOV);
else
aForm.Controls.Add(aLOV); Declare aLOV as a field in your class - most likely a private field like all the other controls.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Great thats works... THANKS
but there's something... weird.
the class show only flashes on the first load, but normal after that. is there anyway to solve this ???
THANKS... a lot
|
|
|
|
|
Bhangorix wrote:
the class show only flashes on the first load, but normal after that
I don't understand what you mean? If you mean you want it to be loaded initially, then instantiate it and add it to the controls collection in your constructor (or, like the VS.NET designer does it, in InitializeComponent ).
If this isn't what you mean, please elaborate.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
i mean... when this proc
if (aLOV == null)
aLOV = new MyDataGrid();
if (aForm.Controls.Contains(aLOV))
aForm.Controls.Remove(aLOV);
else
aForm.Controls.Add(aLOV);
call for the the first time, it just flashes.
but never mind, i've work something else that is different but a litle like that one. THANKS anyway
|
|
|
|
|
Basically, i want to know if there is a way to convert an Image to an Icon in C#. I tried the Icon constructor which accepts a Stream object and a Memory stream accepts a byte[], but i wasnt sure how to convert an Image to a byte[].
I am loading a .png file into memory to show a status, but i also have to show the same image in the form's icon and i dont want to have a .ico and a .png file for every status.
Thanks for the help.
|
|
|
|
|
Why don't you load an icon and convert it to a bitmap? Icon.ToBitmap does it.
(Don't know how to do it the other way round...)
|
|
|
|
|
Mybe Im just stupid, but I cant work out how to insert a new line in the text I wish to enter into a textBox (multiline = true) on the COMPACT FRAMEWORK.
\r \n, nope , I need some help.
tia
|
|
|
|
|
Greets,
You should be able to insert a newline as a CR/LF pair, but I would use the System.Environment.Newline static member to add the newline based on the environment.
If you're referring to adding a newline while entering text into one, perhaps Ctrl-Enter may help.
Regards,
Joe
|
|
|
|
|
When a Windows app starts, it typically calls WinMain where it passes the desired window state as the 4th parameter (nCmdShow).
How is this value retrieved in .NET? I've searched the Application object and it doesn't seem to be there.
Thanks!
Alvaro
Can I ask you a question?
|
|
|
|
|
Check the Form.WindowState property of your application form (main form). If you read the documentation for the property, however, it'll tell you that the initial value is always FormWindowState.Normal until the Window is shown.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
If you read the documentation for the property, however, it'll tell you that the initial value is always FormWindowState.Normal until the Window is shown.
So then how can I retrieve it before the window is shown? Surely the runtime must keep it somewhere so it can change the WindowState property right before showing the form. Perhaps there's a Win32 API that holds this information about a process, but I can't find it.
Thanks,
Alvaro
Can I ask you a question?
|
|
|
|
|
The runtime doesn't expose near as much as what Win32 would. Otherwise ListViews would support grouping, working areas, multiple column sorting, and filter columns. It doesn't. Forms would support drop-shadows. They don't. In these and many other cases, you have to P/Invoke code and often defines structs and enums/consts. These are wrapper classes - and early ones at that.
If you need the window state before the application window (main form) is shown in a purely .NET manner, you're sunk.
You could P/Invoke GetWindowPlacement and redefine the WINDOWPLACEMENT struct in the Windows Manager functions (see MSDN Library for this function for more information). It requires an HWND , which you can easily get from Form.Handle (inherited from Control ). Something like this:
[DllImport("user32.dll")]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement placement);
internal struct WindowPlacement
{
public int length;
public int flags;
public int showCmd;
public POINT ptMinPosition;
public POINT ptMaxPosition;
public RECT rcNormalPosition;
}
internal POINT
{
public long x;
public long y;
}
internal RECT
{
public long left;
public long top;
public long right;
public long buttom;
}
WindowPlacement placement = new WindowPlacement();
placement.cbSize = Marshal.SizeOf(placement);
if (GetWindowPlacement(this.Handle, ref placement))
{
int nCmdShow = placement.showCmd;
}
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
The runtime doesn't expose near as much as what Win32 would. Otherwise ListViews would support grouping, working areas, multiple column sorting, and filter columns. It doesn't. Forms would support drop-shadows. They don't. In these and many other cases, you have to P/Invoke code and often defines structs and enums/consts. These are wrapper classes - and early ones at that.
What's the explanation for all these things lacking? I mean, on one hand you'd think it's because they didn't have sufficient time to include everything. While on the other, it looks like a ploy to keep our apps tied to the OS, diminishing the possibility of one day running them on other platforms.
I guess the important question is, is Microsoft planning on filling the missing pieces in future releases?
Heath Stewart wrote:
You could P/Invoke GetWindowPlacement and redefine the WINDOWPLACEMENT struct in the Windows Manager functions (see MSDN Library for this function for more information). It requires an HWND, which you can easily get from Form.Handle (inherited from Control).
That did it! Thanks for taking the time to answer Heath.
Regards,
Alvaro
He who laughs last, thinks slowest.
|
|
|
|
|