|
blackblizzard wrote: someMethod(this.GetType());
Try this.
someMethod(typeof(MyClass)); Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
No, sorry, the problem is I'm emitting the code with Reflection.Emit, so I can't just type "MyClass".
It works like this:
* Someone calls my class ObjectFactory with a Type they want me to extend, say "MyType".
* Using reflection, I create a subclass of MyType, called EXT_MyType, which has some extra fields and methods. I want to add a static initializer to EXT_MyType, which should look like this:
static EXT_MyType() {
someMethod(typeof(EXT_MyType));
}
But the problem is when I emit the code for this static initializer I haven't yet created the type EXT_MyType (that's what I'm doing, I'm creating its static initializer, etc), so I don't know how to emit this code. See, I'm trying to emit it like this:
ilGenerator.Emit(OpCodes.Ldtoken, typeof(EXT_MyType));
ilGenerator.Emit(OpCodes.Call, typeof(EXT_MyType).GetMethod("someMethod"));
but EXT_MyType doesn't exist yet. So I'm wondering whether there's a primitive or something I can invoke from within the static initializer that will allow me to do this instead:
static EXT_MyType() {
someMethod(getTypeOfThisClass());
}
Where getTypeOfThisClass() would be that "primitive or something...". Since this call would take place once I've created EXT_MyType there wouldn't be a problem. I just don't know whether a primitive like that exists.
If it doesn't, I'd appreciate suggestions on alternative ways of doing this.
Thanks!
|
|
|
|
|
There's no such primitive. You'll have to emit a typeof(EXT_MyType).
This doesn't mean that typeof(EXT_MyType) has to appear in your code - you can get the System.Type instance in other ways using the Reflection API.
In fact, I think you already are using that System.Type instance: System.Reflection.Emit.TypeBuilder derives from System.Type.
So you should be able to use:
ilGenerator.Emit(OpCodes.Ldtoken, myTypeBuilder);
|
|
|
|
|
Yeah, thanks, I think that's it. I had actually found a solution:
ilGenerator.Emit(OpCodes.Ldtoken, classInitializer.DeclaringType);
but of course yours is better, since there's no point doing it like that when I already have the type builder. It was a newbie question
Thanks!
(I was going to post my own solution but I'm still having some (hopefully unrelated) issues, so I was waiting to see if it really did solve the problem; I'll mark the thread as solved, though)
EDIT: It worked! Thanks
|
|
|
|
|
Hi experts
i am working with winform using C#.NET
Can you Help Me that if i am Create ADO.NET Connection
like
using(SqlConnection ICon=new new SqlConnection()
{
ICon.Open();
}
if i am using this connection in 100 form then i have every time open that connection in every form or any other way to manage this.
dinesh
|
|
|
|
|
Sharma Dinesh Kumar wrote: if i am using this connection in 100 form then i have every time open that connection in every form or any other way to manage this.
..or you create a small method that gives you an new and open connection;
SqlConnection GimmeConnection()
{
SqlConnection con = new SqlConnection(connectionString);
con.Open;
return con;
} ..used like this;
Using (SqlConnection = GimmeConnection)
{
} I are Troll
|
|
|
|
|
Hi everyone.
I'm having trouble with the following: I need a way to distinguish the different fields of a class by means of integers -- for example, given the following class:
class MyClass {<br />
<br />
float floatField;<br />
double[] arrayField;<br />
Object objectField;<br />
<br />
}
I'd like to be able to identify floatField as field 0, arrayField as field 1, and objectField as field 2. I don't really care about the order, but I need the integers to be consecutive and to never change throughout execution. They can change between executions.
I haven't seen any straightforward method for this in Type . The best I can come up with is to call typeof(MyClass).GetFields() and use the order the fields in the return array as their id. For example, if it returns [floatField, arrayField, objectField] , it would return in the ids I mentioned above (0, 1 and 2, respectively).
I'm going to need to find the id of a given field pretty often throughout execution, so I create a dictionary that maps the name of a field to its index, like so:
int i = 0;
Dictionary<string, int> fieldOffsetDict = new Dictionary<string, int>();
foreach (FieldInfo field in typeof(MyClass).GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) {
fieldOffsetDict[field.Name] = i++;
}
Anyone can think of a better way of doing this (for example, is there a method that I've failed to see that would do this directly), or do I keep it as it is? I can't help but feel this is a bit of an overkill, so I'd like to make sure.
Also, if I didn't mind the ids not being consecutive, would there be a better way to do this?
Thanks.
|
|
|
|
|
Hi,
if you want a free choice for the field names, and need name->ID mapping, the dictionary seems best; for a simple ID->name mapping, a List would suffice.
if you don't really care about field names, then why not name them consecutively, hence:
class MyClass {
float f0;
double[] f1;
Object f2;
}
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Thanks for the confirmation.
I do care about field names, just not about their order. The code still needs to be legible
|
|
|
|
|
Hello!
I have a problem with some WPF controls.
For example I created a derived from Window tooltip, that will display the .xaml instance of MenuButtons. MenuButtons just simply containts 2 buttons that can be clicked by the user. That's the code :
(menuContentTooltip is a class derived from Window, and that will contain my WPF .xaml MenuButtons control, this menuContentTooltip is a global variable)
if (menuContentTooltip != null)
{
menuContentTooltip.Close();
menuContentTooltip = null;
}
MenuButtons menuButtons = new MenuButtons();
var ttp = new ToolTipParams
{
ParentFrameworkEl = menuBtn,
ChildControlInToolTip = menuButtons,
Title = "Menu",
PositionOfToolTip = ToolTipParams.Position.Below
};
menuContentTooltip = new ToolTip(ttp, false, null);
if (menuContentTooltip.ChildControl != null)
{
((MenuButtons) menuContentTooltip.ChildControl).DemmarerButton.MouseLeftButtonUp += delegate
{
if (menuContentTooltip != null)
menuContentTooltip.Close();
};
((MenuButtons) menuContentTooltip.ChildControl).GestionButton.MouseLeftButtonUp += delegate
{
if (menuContentTooltip != null)
menuContentTooltip.Close();
};
menuContentTooltip.ShowToolTipIfNotAuto();
}
The problem is that everytime when I click the button that executes the code above, the application memory increases without reason, and if I do memory dumping, loading SOS.dll, I see more and more objects of MenuButtons type. For example I clicked and closed the tooltip 3 times, then in visual studio I see this:
!dumpheap -mt 036424f0
Address MT Size
0eabe738 036424f0 264
0ec03568 036424f0 264
0ed03fe0 036424f0 264
total 3 objects
Statistics:
MT Count TotalSize Class Name
036424f0 3 792 MenuButtons
Total 3 objects
!gcroot 0eabe738
Note: Roots found on stacks may be false positives. Run "!help gcroot" for
more info.
Error during command: Warning. Extension is using a callback which Visual Studio does not implement.
Scan Thread 3692 OSTHread e6c
Scan Thread 820 OSTHread 334
Scan Thread 4040 OSTHread fc8
Scan Thread 1804 OSTHread 70c
Scan Thread 2780 OSTHread adc
Scan Thread 3128 OSTHread c38
Scan Thread 3352 OSTHread d18
Scan Thread 2388 OSTHread 954
Scan Thread 3260 OSTHread cbc
Scan Thread 3908 OSTHread f44
Scan Thread 1264 OSTHread 4f0
Scan Thread 2516 OSTHread 9d4
Scan Thread 2760 OSTHread ac8
Scan Thread 2828 OSTHread b0c
Scan Thread 1816 OSTHread 718
Scan Thread 2500 OSTHread 9c4
Scan Thread 3156 OSTHread c54
Scan Thread 1424 OSTHread 590
Scan Thread 2680 OSTHread a78
Scan Thread 2900 OSTHread b54
Scan Thread 4004 OSTHread fa4
Scan Thread 3668 OSTHread e54
Scan Thread 3032 OSTHread bd8
Scan Thread 2536 OSTHread 9e8
Scan Thread 2580 OSTHread a14
Scan Thread 436 OSTHread 1b4
Scan Thread 2040 OSTHread 7f8
Scan Thread 1912 OSTHread 778
Scan Thread 2540 OSTHread 9ec
Scan Thread 1916 OSTHread 77c
DOMAIN(0015DB80):HANDLE(WeakSh):963878:Root:0ebc0514(System.EventHandler)->
0ebc0250(System.Windows.Documents.AdornerLayer)->
0ebc0158(System.Windows.Documents.AdornerDecorator)->
0ebc054c(System.Windows.Controls.ContentPresenter)->
0eb639c4(System.Windows.Controls.Grid)->
0eb63b08(System.Windows.Controls.UIElementCollection)->
0eb63b1c(System.Windows.Media.VisualCollection)->
0eb67a58(System.Object[])->
0eb67948(System.Windows.Controls.Grid)->
0eb67ac4(System.Windows.Controls.UIElementCollection)->
0eb67ad8(System.Windows.Media.VisualCollection)->
0eb67bf8(System.Object[])->
0eb67af0(System.Windows.Controls.StackPanel)->
0eb67ee8(System.Windows.Controls.UIElementCollection)->
0eb67efc(System.Windows.Media.VisualCollection)->
0ebbf5e8(System.Object[])->
0eabe738(MenuButtons)
!gcroot 0ec03568
Note: Roots found on stacks may be false positives. Run "!help gcroot" for
more info.
Error during command: Warning. Extension is using a callback which Visual Studio does not implement.
Scan Thread 3692 OSTHread e6c
Scan Thread 820 OSTHread 334
Scan Thread 4040 OSTHread fc8
Scan Thread 1804 OSTHread 70c
Scan Thread 2780 OSTHread adc
Scan Thread 3128 OSTHread c38
Scan Thread 3352 OSTHread d18
Scan Thread 2388 OSTHread 954
Scan Thread 3260 OSTHread cbc
Scan Thread 3908 OSTHread f44
Scan Thread 1264 OSTHread 4f0
Scan Thread 2516 OSTHread 9d4
Scan Thread 2760 OSTHread ac8
Scan Thread 2828 OSTHread b0c
Scan Thread 1816 OSTHread 718
Scan Thread 2500 OSTHread 9c4
Scan Thread 3156 OSTHread c54
Scan Thread 1424 OSTHread 590
Scan Thread 2680 OSTHread a78
Scan Thread 2900 OSTHread b54
Scan Thread 4004 OSTHread fa4
Scan Thread 3668 OSTHread e54
Scan Thread 3032 OSTHread bd8
Scan Thread 2536 OSTHread 9e8
Scan Thread 2580 OSTHread a14
Scan Thread 436 OSTHread 1b4
Scan Thread 2040 OSTHread 7f8
Scan Thread 1912 OSTHread 778
Scan Thread 2540 OSTHread 9ec
Scan Thread 1916 OSTHread 77c
Scan Thread 3812 OSTHread ee4
Scan Thread 1244 OSTHread 4dc
Scan Thread 2740 OSTHread ab4
Scan Thread 3808 OSTHread ee0
Scan Thread 536 OSTHread 218
Scan Thread 3148 OSTHread c4c
Scan Thread 2908 OSTHread b5c
Scan Thread 3436 OSTHread d6c
Scan Thread 3236 OSTHread ca4
Scan Thread 4044 OSTHread fcc
Scan Thread 3780 OSTHread ec4
Scan Thread 1352 OSTHread 548
Scan Thread 1140 OSTHread 474
Scan Thread 604 OSTHread 25c
DOMAIN(0015DB80):HANDLE(WeakSh):96387c:Root:0ece0858(System.EventHandler)->
0ece0594(System.Windows.Documents.AdornerLayer)->
0ece049c(System.Windows.Documents.AdornerDecorator)->
0ece0890(System.Windows.Controls.ContentPresenter)->
0ec96634(System.Windows.Controls.Grid)->
0ec96778(System.Windows.Controls.UIElementCollection)->
0ec9678c(System.Windows.Media.VisualCollection)->
0ec9a6c8(System.Object[])->
0ec9a5b8(System.Windows.Controls.Grid)->
0ec9a734(System.Windows.Controls.UIElementCollection)->
0ec9a748(System.Windows.Media.VisualCollection)->
0ec9a868(System.Object[])->
0ec9a760(System.Windows.Controls.StackPanel)->
0ec9ab58(System.Windows.Controls.UIElementCollection)->
0ec9ab6c(System.Windows.Media.VisualCollection)->
0ecdffc4(System.Object[])->
0ec03568(MenuButtons)
!gcroot 0ed03fe0
Note: Roots found on stacks may be false positives. Run "!help gcroot" for
more info.
Error during command: Warning. Extension is using a callback which Visual Studio does not implement.
Scan Thread 3692 OSTHread e6c
Scan Thread 820 OSTHread 334
Scan Thread 4040 OSTHread fc8
Scan Thread 1804 OSTHread 70c
Scan Thread 2780 OSTHread adc
Scan Thread 3128 OSTHread c38
Scan Thread 3352 OSTHread d18
Scan Thread 2388 OSTHread 954
Scan Thread 3260 OSTHread cbc
Scan Thread 3908 OSTHread f44
Scan Thread 1264 OSTHread 4f0
Scan Thread 2516 OSTHread 9d4
Scan Thread 2760 OSTHread ac8
Scan Thread 2828 OSTHread b0c
Scan Thread 1816 OSTHread 718
Scan Thread 2500 OSTHread 9c4
Scan Thread 3156 OSTHread c54
Scan Thread 1424 OSTHread 590
Scan Thread 2680 OSTHread a78
Scan Thread 2900 OSTHread b54
Scan Thread 4004 OSTHread fa4
Scan Thread 3668 OSTHread e54
Scan Thread 3032 OSTHread bd8
Scan Thread 2536 OSTHread 9e8
Scan Thread 2580 OSTHread a14
Scan Thread 436 OSTHread 1b4
Scan Thread 2040 OSTHread 7f8
Scan Thread 1912 OSTHread 778
Scan Thread 2540 OSTHread 9ec
Scan Thread 1916 OSTHread 77c
Scan Thread 3812 OSTHread ee4
Scan Thread 1244 OSTHread 4dc
Scan Thread 2740 OSTHread ab4
Scan Thread 3808 OSTHread ee0
Scan Thread 536 OSTHread 218
Scan Thread 3148 OSTHread c4c
Scan Thread 2908 OSTHread b5c
Scan Thread 3436 OSTHread d6c
Scan Thread 3236 OSTHread ca4
Scan Thread 4044 OSTHread fcc
Scan Thread 3780 OSTHread ec4
Scan Thread 1352 OSTHread 548
Scan Thread 1140 OSTHread 474
Scan Thread 604 OSTHread 25c
DOMAIN(0015DB80):HANDLE(Pinned):9613fc:Root:02331010(System.Object[])->
0ed8dbf0(UserControls.ToolTip)->
0ed03fe0(MenuButtons)
DOMAIN(0015DB80):HANDLE(WeakSh):963880:Root:0ede4040(System.EventHandler)->
0ede3d7c(System.Windows.Documents.AdornerLayer)->
0ed8dbf0(UserControls.ToolTip)
DOMAIN(0015DB80):HANDLE(WeakSh):9613fc:Root:02331010(System.Object[])->
0ed8dbf0(UserControls.ToolTip)
DOMAIN(0015DB80):HANDLE(WeakSh):963880:Root:0ede4040(System.EventHandler)->
0ede3d7c(System.Windows.Documents.AdornerLayer)
DOMAIN(0015DB80):HANDLE(WeakSh):9613fc:Root:02331010(System.Object[])->
0ede3d7c(System.Windows.Documents.AdornerLayer)
DOMAIN(0015DB80):HANDLE(WeakSh):963880:Root:0ede4040(System.EventHandler)->
0ede3d7c(System.Windows.Documents.AdornerLayer)
DOMAIN(0015DB80):HANDLE(WeakSh):9613fc:Root:02331010(System.Object[])->
0ede3d7c(System.Windows.Documents.AdornerLayer)
DOMAIN(0015DB80):HANDLE(WeakSh):963880:Root:0ede4040(System.EventHandler)->
0ede3d7c(System.Windows.Documents.AdornerLayer)
DOMAIN(0015DB80):HANDLE(WeakSh):9613fc:Root:02331010(System.Object[])->
0ede3d7c(System.Windows.Documents.AdornerLayer)
DOMAIN(0015DB80):HANDLE(WeakSh):963880:Root:0ede4040(System.EventHandler)->
0ede3d7c(System.Windows.Documents.AdornerLayer)
What's wrong with this code, can someone explain and help me?
Thank's in advance!modified on Saturday, February 20, 2010 12:18 PM
|
|
|
|
|
This is C# forum. Here on codeproject there is also forum for WPF
As i know about C# and windows form, the only whay to get memory leak is not to dispose unmanaged resources. Image class is such thing. And it would help if you use <pre></pre> tags around code, so that we can see it nicly formated
|
|
|
|
|
hei,
I want to copy one grid view row to other gv. Is there any shorter way like
gv2.rows.add(gv1.rows[1]);
Syed Shahid Hussain
|
|
|
|
|
|
Hi,
I have a datagridview upon which I allow the user to re-order the columns. I then store the column order in a sqlServer database table. When the user starts the datagridview my code reads the database and retreives the user's column positions.
The code loops through the columns and sets the displayIndex as previously saved. No problems except the columns are displayed in the wrong order The effect is rather inconsistent and not all the columns are in the wrong order.
Under the debugger I can see the displayindices are correctly set as per the database table. The columns just appear in the wrong place on-screen
I have a fix I simply run the code that sets the displayIndices twice. The problem goes away (well up to now).
I wonder if anyone has had similar displayIndex problems and has a better fix? I have one or two comments in my code saying //DONT REMOVE THIS CODE. IT LOOKS WRONG BUT IS FIXING AN ISSUE.
Terry
|
|
|
|
|
Can you share the solution?
|
|
|
|
|
Hi,
I happend to have exactly the same strange problem! Unfortunately I have no better solution.
Has anyone else figured this one out or is it a DataGrid bug?
/Lars
|
|
|
|
|
Suppose your DataGridView has the columns A, B, C, but they should be displayed as B, A, C.
The desired column order will be restored from this table:
Column name Index DisplayIndex
A 0 1
B 1 0
C 2 2
Before you loop thru this table sort it by DisplayIndex:
Column name Index DisplayIndex
B 1 0
A 0 1
C 2 2
|
|
|
|
|
I'm trying to write a method to find out if a workbook is empty or not..
I'm using Excel 2007 and Microsoft.Office.Interop.Excel.Workbook.
If the workbook has content in more than one cell I'm able to detect this, but if I have an empty "workbookA" and a "workbookB" with content in cell A1 I'm not able to seperate these..
Does anyone have a hot tip? (It's hard to find anything on excel-automation on google..)
private bool IsWorkbookEmpty(Workbook excelBook)
{
try
{
if (excelBook.Sheets.Count <= 0)
{
return true;
}
else
{
foreach (Worksheet sheet in excelBook.Sheets)
{
Range excelRange = sheet.UsedRange;
int test1 = excelRange.Columns.Count;
int test2 = excelRange.Rows.Count;
int test3 = excelRange.Count;
if (test1 > 1 || test2 > 1 || test3 > 1)
{
return false;
}
else
{
foreach(Range cell in excelRange)
{
object test5 = cell.FormulaR1C1;
object test6 = cell.get_Value(Type.Missing);
}
}
}
}
}
catch (Exception)
{
return false;
}
return true;
}
|
|
|
|
|
Figured it out
private bool IsWorkbookEmpty(Workbook excelBook)
{
try
{
if (excelBook.Sheets.Count <= 0)
{
return true;
}
else
{
foreach (Worksheet sheet in excelBook.Sheets)
{
Range excelRange = sheet.UsedRange;
int test1 = excelRange.Columns.Count;
int test2 = excelRange.Rows.Count;
int test3 = excelRange.Count;
if (test1 > 1 || test2 > 1 || test3 > 1)
{
return false;
}
else
{
foreach(Range cell in excelRange)
{
if (cell.Value2 != null)
{
string cellValue = cell.Value2.ToString();
if (cellValue.Trim().Length > 0)
return false;
}
}
}
}
}
}
catch (Exception)
{
return false;
}
retur
|
|
|
|
|
WorkSheets = all your Worksheets
private void deleteUnUsedWorksheets()
{
foreach (Excel._Worksheet sheet in WorkSheets)
{
if (IsWorkbookEmpty(sheet))
sheet.Delete();
}
}
private bool IsWorkbookEmpty(Excel._Worksheet sheet)
{
ExcelRange = sheet.UsedRange;
if (ExcelRange.Columns.Count > 1 |
ExcelRange.Rows.Count > 1 |
ExcelRange.Count > 1)
return false;
else
return true;
}
You dont have to check each cell. Its senseless and you throw a lot of memory
Don`t throw a catch if its only a return false!
|
|
|
|
|
There are a some rectangular controls on a winform on which a dragdrop can happen.
When an item (which has a numeric value) is dragged over any of these controls, the control should show possible result by changing its shape (Increase area by numeric value).
This should be temporary. In case i move the item it out of the control without dropping, this change should revert back. Only in case the drop is done, the change should be permanent.
Now, this effect can be achieved using DragEnter and DragLeave. Currently, I am making the modification to the Control two times by adding the changes and removing the changes.
What I want to know now is whether it is possible to save the state of control so that it can be reverted back to the old state.
In reality, the changes are complicated than just adding area into the shape. But the concept remains same. Control needs to change temporarily and a simple method to restore its state needs to be achieved.
Is it possible?
|
|
|
|
|
Hi,
I would derive new Control types that do this, so make a ReshapableButton inheriting from Button, etc. And give them a boolean Reshaped property.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
OK. This is already there. Whenever there is a DragLeave event, the control checks if it was reshaped and then it draws itself once again after making changes back.
My question is how to save the details of control during DragEnter which can be easily resumed at DragLeave.
Serialization could be one solution. However implementing that within a class to handle itself, seems a bit tricky. Forms cannot be serialized easily.
So, basically I need a control that can save its state by serializing itself. When needed, it resumes from the serialized data. The fact that the control is on UI thread and it has to be a self managed code makes it tricky. I just need a clue how to implement this.
|
|
|
|
|
Hi,
I am looking for a " student tracking system" project using c#.net and sql server.
Let me know if there is any useful links.
Regards,
Savitha
|
|
|
|
|
savitha87k wrote: I am looking for a " student tracking system" project using c#.net and sql server.
Stop looking and start coding
savitha87k wrote:
Let me know if there is any useful links.
We are not here to find exsisting project. We are here to help for a specific code problem
Edit: typo
|
|
|
|