|
In many cases (like yours) no conversion is necessary; if I have an instance of class X, but pass it in as Object, and cast it back to X, then there is no conversion -- that's what I meant.
blackblizzard wrote: depends on whether the type of the object has a copy constructor
I don't think you said that originally, but I see what you mean.
I also tested it and see that you are correct; Invoke doesn't seem to behave as documented. Now I'm intrigued...
|
|
|
|
|
PIEBALDconsult wrote: In many cases (like yours) no conversion is necessary; if I have an instance of class X, but pass it in as Object, and cast it back to X, then there is no conversion -- that's what I meant.
Ah, alright. Well, that's good news. Although I'd still like to know how to do it without the cast, just for sake of knowing
PIEBALDconsult wrote: I don't think you said that originally, but I see what you mean.
True, I didn't. I didn't want to include extraneous info, and I was asking something very specific... But yeah, I forgot you didn't know about that.
PIEBALDconsult wrote: I also tested it and see that you are correct; Invoke doesn't seem to behave as documented. Now I'm intrigued...
Great. Now that makes two of us
|
|
|
|
|
blackblizzard wrote: I'd still like to know how to do it without the cast
I tried to write overloaded methods -- one for ConstructorInfo, one for MethodInfo -- but it didn't compile; it might with C# v4.0
Still, even if it worked there'd be the extra method call which would likely be slower than the cast.
|
|
|
|
|
Aha! It is documented:
"
Return Value
Type: System..::.Object
An object containing the return value of the invoked method, or nullNothingnullptra null reference (Nothing in Visual Basic) in the case of a constructor.
"
So I think you have to cast.
Here's a little something I whipped up to show a generic method to do it:
private static readonly System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodBase> dic ;
static Test
(
)
{
dic = new System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodBase>() ;
return ;
}
public static T
Clone<T>
(
this T Source
)
where T : class
{
System.Type t = typeof(T) ;
System.Reflection.MethodBase m ;
if ( !dic.ContainsKey ( t ) )
{
System.Type[] part = new System.Type[] { t } ;
m = t.GetMethod ( "Clone" , part ) ;
if ( ( m == null ) || ( ((System.Reflection.MethodInfo) m).ReturnType != t ) )
{
m = t.GetConstructor ( part ) ;
if ( m == null )
{
throw ( new System.Exception ( "blah blah blah" ) ) ;
}
}
dic [ t ] = m ;
}
else
{
m = dic [ t ] ;
}
T result ;
if ( m.IsConstructor )
{
result = (T) ((System.Reflection.ConstructorInfo) m).Invoke ( new object[] { Source } ) ;
}
else
{
result = (T) m.Invoke ( Source , new object[] { Source } ) ;
}
return ( result ) ;
}
|
|
|
|
|
PIEBALDconsult wrote: An object containing the return value of the invoked method, or nullNothingnullptra null reference (Nothing in Visual Basic) in the case of a constructor."
So I think you have to cast.
Ah, I see. That's a bit weird, IMHO, but there you go . Good to get that out of the way, thanks for the info
As for your code, yep, that's pretty much what I was doing except for the generics. However the caller doesn't know the type of the object it's going to clone, so I'll keep it non-generic. But thanks for the alternative
Finally, they gave me a very good answer at Stack Overflow[^] using Func<,> instead of MethodBase . It's way more elegant (and very probably more efficient) so I'm adopting that solution.
Thanks for your input
(so many smiley faces! but there you go)
|
|
|
|
|
Hi,
I use VS2010 + windows forms for a few months, so i can't try WPF, my application is too developped to start from 0.
So I would like to know how to totally edit the skins of combobox, buttons...
To not have windows XP's theme for example.
For example, if I have all my design on Photoshop, I should edit skins of combobox in order to do the same as the design.
I've seen some application which were doing that, but they were really expensive.
So I ask you: How i can create a skin? or edit vista's Style, to have a comboBox totally personnalized.
Thanks.
|
|
|
|
|
[^]
Dear friends
How to send group mail using c# coding.
Sir/Mam,
I'm Kannan.I Like very much for the coding for the project
THANKNG YOU
KANNAN
modified on Tuesday, March 30, 2010 7:37 AM
|
|
|
|
|
If you want to send a mail to group in that case the group should have a defined email address in the Global Address List. Set the group email address in To list of the email.
If you are looking for code to send email using c# then you can check this article in CP
Send mail through C#[^]
WWW, WCF, WWF, WPF, WFC .... WTF
|
|
|
|
|
If you are building a windows app, see here[^].
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...
|
|
|
|
|
|
hello
I've couple core 32 bit dll - trying to package them under a Windows Service running on 64 bit Windows. Is this feasible?
Thanks
dev
|
|
|
|
|
A 32 bit application can run on 64-bit windows, these applications are handled in a different way, but the communication between 32-bit and 64 bit modules can’t happen directly (You need to do some workaround for that). It will be better if you try to migrate it to 64-bit.
WWW, WCF, WWF, WPF, WFC .... WTF
|
|
|
|
|
Change the platform of your service to x86 (instead of Any CPU or x64), then you will not have those communication problems with the 32 bit dlls.
|
|
|
|
|
my application exports a .doc file.. I want to know can I check if any .doc application was installed, eg MS Word, OpenOffice, etc...
|
|
|
|
|
1- Check for the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Office , if this exists then MS Office is installed in the system or you can check if WINWORD.EXE file exists.
2- For wordpad check WordPad.exe .
3- Same approach you can use for open office, for Open office there must by some executable file.
WWW, WCF, WWF, WPF, WFC .... WTF
|
|
|
|
|
I am using this code to open an adobe reader document but I want to know how can I set the start size to maximized?
Process.Start(Application.StartupPath + "\\Bahrain2030_English.pdf");
|
|
|
|
|
Based on the following, it doesn't seem like you've got anything provided via command arguments (Adobe Reader[^]).
It looks like you'll have to find the reader process and using pinvokes (like this[^]) to maximize the window.
|
|
|
|
|
try something in this format:
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(Application.ExecutablePath + "\\Bahrain2030_English.pdf");
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
System.Diagnostics.Process.Start(info);
|
|
|
|
|
I am uisng this code to read a specific node from XML using XMLDocument but I want to use the Dataset.ReadXML and want to know how can I do the same but using Dataset?
Save the Value to XML:
resume_xml_document.SelectSingleNode("/Resume/Detail/FullName").InnerText = PersonalInfoForm.txtFullName.Text;
Getting the Value from XML:
txtFullName.Text = resume_xml_document.SelectSingleNode("/Resume/Detail/FullName").InnerText;
|
|
|
|
|
Greetings, all...
I'm using Visual Studio 2008 Express with C# and SQL Server 2008 backend. I'm wanting to design a lookup table form where the user can select the table from a list of items on the left (probably a TreeView with a node for each entry), then have the grid on the right reflect the contents of that table (Users, Config settings, Security, etc.). But I don't have to have a table adapter for each table that could be on the form and would like to re-use the component by setting the properties on the fly. Has anyone done this before?
As a corrolary to this, if the user double-clicks on an entry in the grid, I would like to pop-up a form to allow the user to edit that entry. Again, I would like to use a single form to do this, without having a different form and table adapter, binding source, etc., for each possible table.
Has anyone done this before and can offer any suggestions? Or would the code to do it all run-time outweigh what it would take to just have separate table adapters and forms for each possible lookup table?
Thanks in advance!
- Bert
|
|
|
|
|
Hi all,
I'm trying to expand the code here:
http://www.fryan0911.com/2009/05/how-to-read-pop3-email-using-c.html[^]
so that previosly read messages aren't read over again. I thought I could make this work by storing previosly read messages Id's in a text file and compare these Ids to the message Ids retrieved from the server. The problem I'm having is when trying to assign a message id to the sLine variable as you can see below:
StreamReader objReader = new StreamReader(readIDPath + "readIDs");
POPMessageId sLine;
int i = 0;
while (objReader != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
{
readIDs[i] = sLine;
i++;
}
}
objReader.Close();
The error I get is:
Error: Cannot convert type 'string' to 'SmtPop.POPMessageId'
I've tried casting the string to type POPMessageId but that doesn't fix it. Is there a way I can store the Id's in the file as type PopMessageId or retrieve them as this type? I'm new to working with files in C# but I'm sure it must be possible.
Hope you can help.
Mel
|
|
|
|
|
Of course you're getting an error, a string is not a POPMessageId nor vise versa.
What is POPMessageId? (No, I'm not downloading the library to find out)
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Thanks for your reply Mark.
I looked at the POPMessageId and it takes in two ints (Id & size):
POPMessageId(Int Id, Int size)
This helped me resolve my problem as I then just converted the string taken from the text file to type Int32 and added it to the Id of my sLine POPMessageId object.
sLine.Id = Convert.ToInt32(objReader.ReadLine());
Since opening this thread I was able to complete what I set out to do, which was to keep track of previously read emails on a POP3 server.
I could have used:
http://www.example-code.com/csharp/pop3_unread.asp[^]
but I wanted to give this a shot myself. Plus I didn't like the "30-day trial"
Thanks again
Mel
|
|
|
|
|
Glad it helped point you in the right direction.
Now to find the moron who voted 1
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Well I just voted it as a good answer.
Hope that brings the score up a little.
Thanks again
|
|
|
|
|
Hi,
I am trying to build an IE BHO in C# for taking the snapshot of a webpage loaded in the IE browser. Here is what I'm trying to do:
public class ShowToolbarBHO : BandObjectLib.IObjectWithSite
{
IWebBrowser2 webBrowser = null;
public void SetSite (Object site)
{
.......
if (site != null)
{
......
webBrowser = (IWebBrowser2)site;
......
}
}
}
Also, I p/invoke the following COM methods:
[Guid("0000010D-0000-0000-C000-000000000046")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[ComImportAttribute()]
public interface IViewObject
{
void Draw([MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.LPStruct)] ref COMRECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, int dwContinue);
int GetColorSet([MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hicTargetDev, [Out] IntPtr ppColorSet);
int Freeze([MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, out IntPtr pdwFreeze);
int Unfreeze([MarshalAs(UnmanagedType.U4)] int dwFreeze);
int SetAdvise([MarshalAs(UnmanagedType.U4)] int aspects, [MarshalAs(UnmanagedType.U4)] int advf, [MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink);
void GetAdvise([MarshalAs(UnmanagedType.LPArray)] out int[] paspects, [MarshalAs(UnmanagedType.LPArray)] out int[] advf, [MarshalAs(UnmanagedType.LPArray)] out IAdviseSink[] pAdvSink);
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class COMRECT
{
public int left;
public int top;
public int right;
public int bottom;
public COMRECT()
{
}
public COMRECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisibleAttribute(true)]
[GuidAttribute("0000010F-0000-0000-C000-000000000046")]
[ComImportAttribute()]
public interface IAdviseSink
{
void OnDataChange([In]IntPtr pFormatetc, [In]IntPtr pStgmed);
void OnViewChange([MarshalAs(UnmanagedType.U4)] int dwAspect, [MarshalAs(UnmanagedType.I4)] int lindex);
void OnRename([MarshalAs(UnmanagedType.Interface)] object pmk);
void OnSave();
void OnClose();
}
Now When I take the snapshot:
I make a call CaptureWebScreenImage((IHTMLDocument2) webBrowser.document);
public static Image CaptureWebScreenImage(IHTMLDocument2 myDoc) {
int heightsize = (int)getDocumentAttribute(myDoc, "scrollHeight");
int widthsize = (int)getDocumentAttribute(myDoc, "scrollWidth");
Bitmap finalImage = new Bitmap(widthsize, heightsize);
Graphics gFinal = Graphics.FromImage(finalImage);
COMRECT rect = new COMRECT();
rect.left = 0;
rect.top = 0;
rect.right = widthsize;
rect.bottom = heightsize;
IntPtr hDC = gFinal.GetHdc();
IViewObject vO = myDoc as IViewObject;
vO.Draw(1, -1, (IntPtr)0, (IntPtr)0, (IntPtr)0, (IntPtr)hDC, ref rect, (IntPtr)0, (IntPtr)0, 0);
gFinal.ReleaseHdc();
gFinal.Dispose();
return finalImage;
}
I am not getting the image of the webpage. Rather I am getting an image with black background. I am not sure if this is the right way of doing it, but I found over the web that IViewObject::Draw method is used for taking the image of a webpage in IE.
I was earlier doing the image capture using the Native PrintWindow() method as mentioned in the following codeproject's page - http://www.codeproject.com/KB/graphics/IECapture.aspx
But the image size is humongous! I was trying to see if I can reduce the size by using other techniques. It would be great if someone can point out the mistakes (I am sure there would be many) in my code above.
Thanks, Kapil
|
|
|
|
|