|
I put a Resource dictionary, declare there the page
and in MenuItem_Click showing the window.
It works..but I am wondering how to do this directly in XAML on click event.
<MenuItem Header="_Options" HorizontalAlignment="Left" Click="MenuItem_Click">
<MenuItem.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</MenuItem.Resources>
</MenuItem>
created a myDictionary.xaml file
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window x:Key="pageOptions" Name="Test">
</Window>
</ResourceDictionary>
|
|
|
|
|
Hi
I need to run the content of the batch file from C#. How to do that?
Content of the batch file:
echo off
\\server-name\directory\PsExec.exe -u domain\user-id -p ****** -d -e cmd.exe /c \\server-name\directory\file.exe
I require this to run a software with admin id in system where user does'nt have admin access.
Please post the complete code with all the namespace details since I'm a beginner.
Note: i don't want to have a batch file, i want to write everything in C#.
|
|
|
|
|
You should use the Process.Start()[^] method.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
This site is not a 'free code supermarket'.
You have your requirements, then start reading/coding ; and come back here when you have a specific question rather than a demand for ready code...
No memory stick has been harmed during establishment of this signature.
|
|
|
|
|
Chose one forum and stick to it[^].
Posting to multiple forums annoys the people that you want to get help from, so it's not a good idea.
Ciao,
luker
|
|
|
|
|
I have a group of classes (following strategy pattern) in my project. In the main function, I receive an enum value from the server and based on that I create an object of the base class type.
I am using switch/case statement to achieve this. I read somewhere that the Open/Closed principle does not allow opening a function to add a new case statement whenever a new class is added.
I am thinking of using a Activator.CreateInstance(). Is there any drawback to it.
Is there any other way to create an object from the enum type?
Adding example below even though it is not a full fledged Strategy pattern
abstract public class Mammal
{
public abstract void MakeSound()
}
class Cat:Mammal
{
public override void MakeSound()
{
Console.WriteLine("Meow");
}
}
class Dog:Mammal
{
public override void MakeSound()
{
Console.WriteLine("Bow");
}
}
Main()
{
MammalTypes mammalType = RecieveValueFromServer();
Mammal mammalBase
switch(mammalType)
{
case MammalTypes.Cat:mammalBase = new Cat()
break;
case MammalTypes.Dog:mammalBase = new Dog()
break;
}
mammalBase.MakeSound()
}
|
|
|
|
|
If you qualify it with the namespace, you can use Reflection to create an object from it's string name:
string myType = "MyNamespace.MyClass";
Assembly asm = Assembly.GetExecutingAssembly();
Type t = asm.GetType(myType);
Object o = Activator.CreateInstance(t, null, null);
I would combine this with this tip: Human readable strings for enum elements[^] to replace the human readable bit with the full namespec on each enum element, and you are done.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Thanks,
Is there any drawback if this method replaces
switch/case statements for determinging type at run time?
|
|
|
|
|
Speed probably - I haven't checked this specific case, but Reflection is never quick!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
OriginalGriff wrote: Reflection is never quick
True, but often you can cache the results, I think this is one such situation.
|
|
|
|
|
OriginalGriff wrote: but Reflection is never quick!
That certainly requires qualification.
Regardless of whether one uses reflection or not the .Net code is still loaded via the same mechanism and called via the same mechanism.
So if there is a measurable difference then it will be due to the proxy nature of the reflection semantics. I would suspect that using an interface/base class would entirely eliminate that.
But even if not I would suppose that one is going to have a very unusual plugin hierarchy for the reflection call itself to have an significant (above noise level) impact on performance.
|
|
|
|
|
The class resolution is not through the same mechanism if you use reflection, and it is much slower. But like you say, it's not like you use it enough to matter in most cases (and if you do, you can save the results in a Dictionary<string, Type> on first use).
|
|
|
|
|
BobJanova wrote: The class resolution is not through the same mechanism if you use reflection,
and it is much slower.
The same mechanism is used to load the assembly and class into the AppDomain regardless of whether reflection or application reference is used.
Class loading is a one time operation so that will not have an impact on any general application.
So best you can be referring to is method invocation. If you use indirect method invocation, every time, then that is proxy mechanism where the proxy mechanism itself has a impact.
Again however you would be hard pressed to measure that with code that actually does anything.
However if you use a direct implementation, such as interface/base class invocation then the call semantics are the same. A class instance in memory is the same regardless of whether the class/assembly was loaded by reflection or via that application loading mechanism.
|
|
|
|
|
Yes. but that depends if you willing to violate the Open/Close principle in the process. It is because you would have to modify the class everytime you wanted to change its implementation. You should use the factory pattern to make the functionality more dynamic. This can either be achieved by way of reflection or being database driven. As for performance with reflection, from tests that I have done. The quickest method of execution is using the DynamicMethod class and generating IL code, and wrapping it in a delegate.
|
|
|
|
|
I think it's obvious that the posted code would be used in a method taking the name as a parameter, not hard coded in a string :P.
|
|
|
|
|
Hi,
If i just cancel the CancellationTokenSource token (that is a parameter of the Task.Factory.StartNew) , does it cancells automatically the task? or i need to check in the code of the task if the IsCancellationRequested of this cancellationToken and stop the task manually there?
|
|
|
|
|
You need code in your Action that checks to see if the IsCancellationRequested property of the token is True.
|
|
|
|
|
winforms sql2008
I have a table
Login(id(int),EmailId(varchar(35),connType(varchar))
where conntype has values like pop3 or imap. consider an user is loggedin. i want to fetch connType value of the logged user to do like this
if(conntypeValue == imap)
{
//code for imap connection
}else
{
//code for pop3 connection
}
how can i do it
modified 22-Jan-12 4:10am.
|
|
|
|
|
What exactly are you trying to do in here?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
that code has been done. just i want to retrieve the single value from the table
|
|
|
|
|
What value? What table? You need to provide some more details about your problem.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
winforms sql2008
I have a table
Login(id(int),EmailId(varchar(35),connType(varchar))
where conntype has values like pop3 or imap. consider an user is loggedin. i want to fetch connType value of the logged user to do like this
if(conntypeValue == imap)
{
//code for imap connection
}else
{
//code for pop3 connection
}
how can i do it
i want to fetch connType column value from the table login where id = userid
|
|
|
|
|
Brilliant! I asked you to explain your problem in more detail and you have just repeated your original question.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
I want to connect with pop3 server or imap server on the basis of the account configured by the user. that account values are in the login table. when the user logged in. i want to fetch the value from the login table column servertype. if that value is pop3 the code of pop3 server connectivity will run.
|
|
|
|
|
Login(id(int),EmailId(varchar(35),connType(varchar))
execute a scalar
select connType from Login where id = 5
or
select connType from EmailId where id = 'noone@example.com'
|
|
|
|