|
Hi, I'm writing some code that will create some static methods for a dynamically created assembly. One method is giving me an InvalidProgramException when run.
The code that I am trying to generate, from C# is:
A simple method that will take a Type, a bool and return true if it finds a string that is paired with that Type.
The code to generate this is:
mb = typeBuilder.DefineMethod("TryGetName",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Static,
typeof(bool), new[] { typeof(Type), typeof(bool), typeof(string).MakeByRefType() });
mb.DefineParameter(1, ParameterAttributes.None, "type");
mb.DefineParameter(2, ParameterAttributes.None, "asCompiled");
mb.DefineParameter(3, ParameterAttributes.Out, "name");
ILGenerator tryGetNameIL = mb.GetILGenerator();
LocalBuilder tryGetName_pairLocal = tryGetNameIL.DeclareLocal(typeof(KeyValuePair<string,>));
LocalBuilder tryGetName_enumLocal = tryGetNameIL.DeclareLocal(typeof (Dictionary<string,>.Enumerator));
LocalBuilder tryGetName_resLocal = tryGetNameIL.DeclareLocal(typeof (bool));
Label tryGetName_endLoopLabel = tryGetNameIL.DefineLabel();
Label tryGetName_startLoopLabel = tryGetNameIL.DefineLabel();
Label tryGetName_endStringReplaceLabel = tryGetNameIL.DefineLabel();
Label tryGetName_endOfMethod = tryGetNameIL.DefineLabel();
tryGetNameIL.Emit(OpCodes.Ldfld, typesFieldBuilder);
tryGetNameIL.EmitCall(OpCodes.Callvirt, typesFieldBuilder.FieldType.GetMethod("GetEnumerator", Type.EmptyTypes), null);
tryGetNameIL.Emit(OpCodes.Stloc, tryGetName_enumLocal);
tryGetNameIL.BeginExceptionBlock();
tryGetNameIL.Emit(OpCodes.Br_S, tryGetName_endLoopLabel);
tryGetNameIL.MarkLabel(tryGetName_startLoopLabel);
tryGetNameIL.Emit(OpCodes.Ldloca_S, tryGetName_enumLocal);
tryGetNameIL.EmitCall(OpCodes.Call, tryGetName_enumLocal.LocalType.GetProperty("Current").GetGetMethod(), null);
tryGetNameIL.Emit(OpCodes.Stloc, tryGetName_pairLocal);
tryGetNameIL.Emit(OpCodes.Ldloca_S, tryGetName_pairLocal);
tryGetNameIL.EmitCall(OpCodes.Call, tryGetName_pairLocal.LocalType.GetProperty("Value").GetGetMethod(), null);
tryGetNameIL.Emit(OpCodes.Ldarg_0);
tryGetNameIL.Emit(OpCodes.Ceq);
tryGetNameIL.Emit(OpCodes.Brfalse_S, tryGetName_endLoopLabel);
tryGetNameIL.Emit(OpCodes.Ldarg_2);
tryGetNameIL.Emit(OpCodes.Ldloca_S, tryGetName_pairLocal);
tryGetNameIL.EmitCall(OpCodes.Call, tryGetName_pairLocal.LocalType.GetProperty("Key").GetGetMethod(), null);
tryGetNameIL.Emit(OpCodes.Stind_Ref);
tryGetNameIL.Emit(OpCodes.Ldarg_1);
tryGetNameIL.Emit(OpCodes.Brfalse_S, tryGetName_endStringReplaceLabel);
tryGetNameIL.Emit(OpCodes.Ldarg_2);
tryGetNameIL.Emit(OpCodes.Dup);
tryGetNameIL.Emit(OpCodes.Ldind_Ref);
tryGetNameIL.Emit(OpCodes.Ldstr, "-");
tryGetNameIL.Emit(OpCodes.Ldstr, "::");
tryGetNameIL.EmitCall(OpCodes.Callvirt, typeof(string).GetMethod("Replace", new [] {typeof(string), typeof(string)}), null);
tryGetNameIL.Emit(OpCodes.Stind_Ref);
tryGetNameIL.MarkLabel(tryGetName_endStringReplaceLabel);
tryGetNameIL.Emit(OpCodes.Ldc_I4_1);
tryGetNameIL.Emit(OpCodes.Stloc, tryGetName_resLocal);
tryGetNameIL.Emit(OpCodes.Leave_S, tryGetName_endOfMethod);
tryGetNameIL.MarkLabel(tryGetName_endLoopLabel);
tryGetNameIL.Emit(OpCodes.Ldloca_S, tryGetName_enumLocal);
tryGetNameIL.EmitCall(OpCodes.Call, tryGetName_enumLocal.LocalType.GetMethod("MoveNext"), null);
tryGetNameIL.Emit(OpCodes.Brtrue_S, tryGetName_startLoopLabel);
tryGetNameIL.BeginFinallyBlock();
tryGetNameIL.Emit(OpCodes.Ldloca_S, tryGetName_enumLocal);
tryGetNameIL.Emit(OpCodes.Constrained, tryGetName_enumLocal.LocalType);
tryGetNameIL.EmitCall(OpCodes.Callvirt, typeof(IDisposable).GetMethod("Dispose"), null);
tryGetNameIL.EndExceptionBlock();
tryGetNameIL.Emit(OpCodes.Ldarg_2);
tryGetNameIL.Emit(OpCodes.Ldnull);
tryGetNameIL.Emit(OpCodes.Stind_Ref);
tryGetNameIL.Emit(OpCodes.Ldc_I4_0);
tryGetNameIL.Emit(OpCodes.Stloc, tryGetName_resLocal);
tryGetNameIL.MarkLabel(tryGetName_endOfMethod);
tryGetNameIL.Emit(OpCodes.Ldloc, tryGetName_resLocal);
tryGetNameIL.Emit(OpCodes.Ret);
And this, when run, creates the following IL:
.method public hidebysig static bool TryGetName(class [mscorlib]System.Type 'type',
bool asCompiled,
[out] string& name) cil managed
{
.maxstack 9
.locals init (valuetype [mscorlib]System.Collections.Generic.KeyValuePair`2<string,class> V_0,
valuetype [mscorlib]System.Collections.Generic.Dictionary`2/Enumerator<string,class> V_1,
bool V_2)
IL_0000: ldfld class [mscorlib]System.Collections.Generic.Dictionary`2<string,class> BlueprintSchema.BlueprintNameMapping::types
IL_0005: callvirt instance valuetype [mscorlib]System.Collections.Generic.Dictionary`2/Enumerator class [mscorlib]System.Collections.Generic.Dictionary`2<string,class>::GetEnumerator()
IL_000a: stloc.1
.try
{
IL_000b: br.s IL_0044
IL_000d: ldloca.s V_1
IL_000f: call instance valuetype [mscorlib]System.Collections.Generic.KeyValuePair`2 valuetype [mscorlib]System.Collections.Generic.Dictionary`2/Enumerator<string,class>::get_Current()
IL_0014: stloc.0
IL_0015: ldloca.s V_0
IL_0017: call instance !1 valuetype [mscorlib]System.Collections.Generic.KeyValuePair`2<string,class>::get_Value()
IL_001c: ldarg.0
IL_001d: ceq
IL_001f: brfalse.s IL_0044
IL_0021: ldarg.2
IL_0022: ldloca.s V_0
IL_0024: call instance !0 valuetype [mscorlib]System.Collections.Generic.KeyValuePair`2<string,class>::get_Key()
IL_0029: stind.ref
IL_002a: ldarg.1
IL_002b: brfalse.s IL_0040
IL_002d: ldarg.2
IL_002e: dup
IL_002f: ldind.ref
IL_0030: ldstr "-"
IL_0035: ldstr "::"
IL_003a: callvirt instance string [mscorlib]System.String::Replace(string,
string)
IL_003f: stind.ref
IL_0040: ldc.i4.1
IL_0041: stloc.2
IL_0042: leave.s IL_0065
IL_0044: ldloca.s V_1
IL_0046: call instance bool valuetype [mscorlib]System.Collections.Generic.Dictionary`2/Enumerator<string,class>::MoveNext()
IL_004b: brtrue.s IL_000d
IL_004d: leave IL_0060
}
finally
{
IL_0052: ldloca.s V_1
IL_0054: constrained. valuetype [mscorlib]System.Collections.Generic.Dictionary`2/Enumerator<string,class>
IL_005a: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_005f: endfinally
}
IL_0060: ldarg.2
IL_0061: ldnull
IL_0062: stind.ref
IL_0063: ldc.i4.0
IL_0064: stloc.2
IL_0065: ldloc.2
IL_0066: ret
}
When run (e.g. calling BlueprintNameMapping.TryGetValue(typeof(Blueprints.cEngineBlueprint), true, out name); where Blueprint.cEngineBlueprint is some class not related to this), I simply get an InvalidProgramException with absolutely no clue as to where the error occured. I have read this occurs when IL is converted to native code. I have narrowed the problem down up to this method and cannot work out where it could be going wrong.
Thanks,
Ben
|
|
|
|
|
Any example code or idea will be highly appreciated.
Thank you.
|
|
|
|
|
DNS takes the responsibility to translate the domain name to IP address. Hence you cannot add the IP to the DNS server simply but register it. If you want to specify a website to a certain IP address, you can modify the c:\windows\system32\drivers\etc\host file.
LuckyBoy
|
|
|
|
|
Iam working on creating a multi-column combo box for myself. I would like coders to be able to use the designer and collection editor dialogs. Right now the collection does not show up in the component's properties panel. What am I doing wrong please? Here's what I have so far ...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.ComponentModel.Design;
using System.Drawing.Design;
namespace MultiColumnComboBox
{
public partial class MultiColumnComboBox : ComboBox
{
public MultiColumnComboBox()
{
InitializeComponent();
}
public MultiColumnComboBox(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public class Column
{
public Column()
{
}
#region private variables
private Color foreColor;
private Color backColor;
private int columnWidth;
private bool autoWidth = false;
private string columnType;
#endregion
public Color ForeColor
{
get { return foreColor; }
set { foreColor = value; }
}
public Color BackColor
{
get { return backColor; }
set { backColor = value; }
}
public int ColumnWidth
{
get { return columnWidth; }
set { columnWidth = value; }
}
public bool AutoWidth
{
get { return autoWidth; }
set { autoWidth = value; }
}
public string ColumnType
{
get { return columnType; }
set { columnType = value; }
}
[Editor(typeof(ColumnCollectionEditor),(typeof( UITypeEditor)))]
public class ColumnCollection : CollectionBase
{
public ColumnCollection()
{
}
public Column this[int index]
{
get { return (Column)this.List[index]; }
set { this.List[index] = value; }
}
public int IndexOf(Column item)
{
return base.List.IndexOf(item);
}
public int Add(Column item)
{
return this.List.Add(item);
}
public void Remove(Column item)
{
this.InnerList.Remove(item);
}
public void CopyTo(Array array, int index)
{
this.List.CopyTo(array, index);
}
public void AddRange(ColumnCollection collection)
{
for (int i = 0; i < collection.Count; i++)
{
this.List.Add(collection[i]);
}
}
public void AddRange(Column[] collection)
{
this.AddRange(collection);
}
public bool Contains(Column item)
{
return this.List.Contains(item);
}
public void Insert(int index, Column item)
{
this.List.Insert(index, item);
}
}
public class ColumnCollectionEditor : CollectionEditor
{
public ColumnCollectionEditor(Type type)
: base(type)
{
}
}
}
}
}
|
|
|
|
|
|
Many thanks for the link Mark - much appreciated.
It sure would be nice if some kind person could correct my existing code so that I could learn from the experience.
|
|
|
|
|
In C++, you do system("command here");
How do you do this in C# without running cmd.exe with an argument?
Thanks.
|
|
|
|
|
Process.Start()
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Process.Start("pause"); gives an error :S.
|
|
|
|
|
What do you expect it to do?
modified 12-Sep-18 21:01pm.
|
|
|
|
|
He expects to run the special commands available in cmd.exe that are not exe files of their own. But he doesn't want to launch cmd.exe itself.
|
|
|
|
|
Like in unmanaged code when you type that it says "Press any key to continue..."
Thats not what I want it to do but its an example.
The school has cmd.exe blocked but I need it to run java commands in my program.
Thanks.
|
|
|
|
|
Then run the java vm with the appropriate command line arguments. Using Process.Start is exactly like typing whatever command your want to run in the Start/Run box, not the CMD window.
|
|
|
|
|
Hi all,
I need to turn offpower-off) the remote computer(Machine) monitor.Can any help me on this scenario.Is it is possible to do by c#.net.
Let me clearly explain me
there are Server and clients...
From server machine I need to turn off(power-off) the client system monitor
kindly help me....
|
|
|
|
|
You can't power down a monitor via code. But you may be able to put the system into sleep mode or maybe turn on the screen saver. Don't ask me how to do this, I'm just providing an idea.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: You can't power down a monitor via code.
Are you sure?[^]
It can be done locally but perhaps not remotely.
|
|
|
|
|
The code sample that is here on CP will work, but only for monitors that support it and only on the local machine. You'll have to write two applications to do this remotely. One app that runs, probably as a service, on each machine you want to control and one that sends the command to the service running on each machine.
|
|
|
|
|
How to send a key stroke from WordPad back to c# app then return control back to wordpad. I try to write a c# app that will catch each key stroke that type in wordpad, then send back some data back to wordpad. Two prgrams working concurrently ?
Example: in wordpad I type a letter "A", c# working in background catch it then send a number "1" back to workpad at focus cursor.
Please help.
|
|
|
|
|
You have to install a keyboard hook from your C# app.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
And what possible legitimate use would this be for. The only uses I can think of are nefarious and no one here will help you. If there is a legitimate case you would be better off creating a C# text editor.
only two letters away from being an asset
|
|
|
|
|
Hi,
On webpage when i click on pdf it's gives "too many arguments".I tried to to paste the pdf then it's working for first time.But again same problem.So what to do this.I am using C# and Oracle.
Thanks
|
|
|
|
|
Apparently, your code, whatever it is, sends too many arguments to Acrobat Reader. Since we know nothing of the code that generates the web page or what the link to your PDF looks like, it's impossible to tell you what you did wrong.
|
|
|
|
|
Hi,
After i paste time it works but after that it won't work.It gives "too many arguments"
A helpful guide that answers important questions, including how COUMADIN® (Warfarin Sodium Tablets, USP) works, diet and drug interactions, and the importance of therapy. Also available in Spanish.
Click here for a printer-friendly version
|
|
|
|
|
Uhhh... you might want to try reposting that, and before you click "Post Message", find and enable the little checkbox that says "Ignore HTML tags in this message..."
|
|
|
|
|
<td width="30%" class="resources termText">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="top">
<a href="pdf/293US08WA03001_v7_UnderstandTherapyBro_WEB.pdf" target="_blank"><img src="img/resources/Understanding-your-COUMADIN-therapy-2008.jpg" alt="Coumadin: Patient Guide" width="52" height="85" border="0" /></a> </td>
<td align="left" valign="top" class="termTextHeader">Understanding Your COUMADIN (Warfarin Sodium Tablets, USP) Therapy Brochure</td>
</tr>
</table>
<p>A helpful guide that answers important questions, including how COUMADIN<sup>®</sup> (Warfarin Sodium Tablets, USP) works, diet and drug interactions, and the importance of therapy. Also available in Spanish.</p>
<p><a href="pdf/293US08WA03001_v7_UnderstandTherapyBro_WEB.pdf" target="_blank">Click here for a printer-friendly version</a></p>
</td>
|
|
|
|
|