Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
QuestionHttpWebRequest question Pin
Ehsan Baghaki24-May-09 19:57
Ehsan Baghaki24-May-09 19:57 
AnswerRe: HttpWebRequest question Pin
Manas Bhardwaj24-May-09 23:19
professionalManas Bhardwaj24-May-09 23:19 
Questionchenge plot in the zedgrapg control Pin
hamidhakimi24-May-09 19:22
hamidhakimi24-May-09 19:22 
AnswerRe: chenge plot in the zedgrapg control Pin
_Maxxx_24-May-09 19:38
professional_Maxxx_24-May-09 19:38 
QuestionWant to validate a function and execute it Pin
Anindya Chatterjee24-May-09 18:54
Anindya Chatterjee24-May-09 18:54 
AnswerRe: Want to validate a function and execute it Pin
Tuwing.Sabado24-May-09 21:14
Tuwing.Sabado24-May-09 21:14 
GeneralRe: Want to validate a function and execute it Pin
Anindya Chatterjee24-May-09 22:12
Anindya Chatterjee24-May-09 22:12 
AnswerRe: Want to validate a function and execute it Pin
S. Senthil Kumar24-May-09 22:56
S. Senthil Kumar24-May-09 22:56 
I guess you're looking for a generic method dispatch mechanism that validates the platform before executing calls. How about something like this.
enum Platform
{
    XP,
    NT,
    Vista
}

class PlatformNameAttribute : Attribute
{
    public PlatformNameAttribute(Platform platForm)
    {
        this.Platform = platForm;
    }

    public Platform Platform { get; set; }
}

class Program
{
    [PlatformName(Platform.NT)]
    static void SomeAPICall()
    {
        Console.WriteLine("NT");
    }

    [PlatformName(Platform.XP)]
    static void SomeOtherAPICall(int x)
    {
        Console.WriteLine("XP");
    }

    static Platform GetCurrentPlatform() { return Platform.NT; }

    static void ExecuteAPICall(Delegate d, params object[] parameters)
    {
        var attributes = d.Method.GetCustomAttributes(typeof(PlatformNameAttribute), false);

        if (attributes != null && attributes.Count() > 0)
        {
            var platformAttribute = (PlatformNameAttribute)attributes[0];

            if (platformAttribute.Platform != GetCurrentPlatform())
            {
                throw new InvalidOperationException("Platform Type mismatch");
            }

            d.DynamicInvoke(parameters);
        }
    }

    static void Main(string[] args)
    {
        ExecuteAPICall(new Action(SomeAPICall));
        ExecuteAPICall(new Action<int>(SomeOtherAPICall), 2);
    }


You could do something more typesafe than DynamicInvoke, with Expression and lambdas, but that would require .NET 3.5, and I'm not sure you are running it.

Regards
Senthil [MVP - Visual C#]
_____________________________
My Home Page |My Blog | My Articles | My Flickr | WinMacro

GeneralRe: Want to validate a function and execute it Pin
Anindya Chatterjee24-May-09 23:32
Anindya Chatterjee24-May-09 23:32 
QuestionThe type initializer threw an exception? Pin
dec8224-May-09 16:57
dec8224-May-09 16:57 
AnswerRe: The type initializer threw an exception? Pin
_Maxxx_24-May-09 19:32
professional_Maxxx_24-May-09 19:32 
GeneralRe: The type initializer threw an exception? Pin
dec8224-May-09 19:50
dec8224-May-09 19:50 
GeneralRe: The type initializer threw an exception? Pin
_Maxxx_25-May-09 12:10
professional_Maxxx_25-May-09 12:10 
AnswerRe: The type initializer threw an exception? Pin
Ramesh Swaminathan24-May-09 19:56
Ramesh Swaminathan24-May-09 19:56 
AnswerRe: The type initializer threw an exception? Pin
S. Senthil Kumar24-May-09 22:36
S. Senthil Kumar24-May-09 22:36 
Questionhow to access CSP? Pin
devvvy24-May-09 16:32
devvvy24-May-09 16:32 
Questionthis as parameter Pin
mark_me24-May-09 16:03
mark_me24-May-09 16:03 
AnswerRe: this as parameter Pin
Luc Pattyn24-May-09 16:07
sitebuilderLuc Pattyn24-May-09 16:07 
GeneralRe: this as parameter Pin
mark_me25-May-09 12:18
mark_me25-May-09 12:18 
GeneralRe: this as parameter Pin
Luc Pattyn25-May-09 12:30
sitebuilderLuc Pattyn25-May-09 12:30 
QuestionDetecting The Debug Version Pin
BlitzPackage24-May-09 15:01
BlitzPackage24-May-09 15:01 
AnswerRe: Detecting The Debug Version Pin
Luc Pattyn24-May-09 16:05
sitebuilderLuc Pattyn24-May-09 16:05 
AnswerRe: Detecting The Debug Version Pin
_Maxxx_24-May-09 16:52
professional_Maxxx_24-May-09 16:52 
AnswerRe: Detecting The Debug Version Pin
BlitzPackage24-May-09 17:11
BlitzPackage24-May-09 17:11 
Question.Net WebBrowser control wont instanciate on form in new thread? Pin
Abydosgater24-May-09 11:51
Abydosgater24-May-09 11:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.