This project has moved and is read-only. For the latest updates, please go here.

[en] Calling static methods? (solved)

Topics: Technical Support
Feb 16, 2016 at 9:50 AM
Edited Feb 16, 2016 at 9:51 AM
Hello Zyan Team,
Hello community,

first, thanks a lot for your great work!

I was wondering if it's possible to call static methods through Zyan?

Normally I declare a proxy with an interfaces like this:
ICANConnect proxy = connection.CreateProxy<ICANConnect>();
But in this case I have 'static' class & methods on server like this:
public static class CAN
    public static bool TransmitDemo()

where is certainly not possible to use them with interfaces.
So, do I have to change code on server and avoid static classes? (e.g. with singleton) or is there a way to call those static class methodes through Zyan? (I hope so)

Thanks a lot for your support
Best regards,
Feb 24, 2016 at 12:37 PM
Edited Feb 24, 2016 at 12:38 PM
Hi Michael,

thank you for your kind words and sorry for my late reply.
Just got back from my vacation.

As for your question: you're right, it's not possible to call static methods via interfaces.
And the only way to call a method in Zyan is via an interface.
So the solution is straightforward: write an interface and a wrapper class to call your static methods.

Let's say, for example, that you have a static class like this:
    static class StaticService
        public static string GeneratePassword(int length)
            var sb = new StringBuilder();
            var rnd = new Random();
            for (var i = 0; i < length; i++)
                var @char = (char)('a' + rnd.Next('z' - 'a'));

            Console.WriteLine("Generated password: {0}, length = {1}",
                sb, length);

            return sb.ToString();
To publish the class for the remote access via Zyan you'll need an interface:
    public interface ISampleService
        string GeneratePassword(int length);
and a non-static wrapper class implementing the interface:
    internal class SampleService : ISampleService
        public string GeneratePassword(int length)
            return StaticService.GeneratePassword(length);
That's all you need.
The complete source code for this example is located here:

Of course, if your application is designed around lots of static classes,
you might end up writing tons of boilerplate code, wrappers and interfaces.

I can see several ways of dealing with that.
  • First, you can get rid of static classes (I'd prefer this one).
  • Second, you may use singletons instead of static classes. Zyan supports singletons natively.
  • Third, you may write a T4-based generator that reflects on your static classes and generates interfaces and wrappers for you. A geeky approach, lots of fun to try.
Regards, Alex
Marked as answer by yallie on 2/29/2016 at 9:24 AM
Feb 29, 2016 at 4:57 PM
Hello Alex,

thank your very much for your answer. No problem, I was on vacation, too ;)

It works very well now. I have rewritten the code as you suggested. Now I have a lot of singleton classes.

Best regards,