IRohitable | .NET, SQL Server, Ruby on Rails and everything related to Software

Google End-to-End Encryption Extension

by Rohit 28. December 2014 12:08

Read about Google's plan to release an end-to-end encryption extension for Chrome. Few months back it was in alpha stage and only code was available for review. It definitely addresses security concerns but I feel end-to-end encryption option should be a part of Gmail internally.

If it is a Chrome extension I am bound to use Chrome and not other browsers, which I dislike.

Tags:

Security

Accessing Interface methods when using explicit Interface implementation

by Rohit 18. December 2014 20:00

I came across a scenario where there are two Interfaces with same method name. For example:

Interface ILocalCustomer
{
       void CustomerDetails();
}

Interface IGlobalCustomer
{
        void CustomerDetails();
}

 

In this scenario there are some challenges:

  1. I need two separate implementations for CustomerDetails but the method name is same in both the Interface.
  2. I want to call the method from another class (other than the class which implements these Interfaces)

The first part of this problem does not involve much trick. You can have explicit Interface implementations like this:

public class Customer : ILocalCustomer,IGlobalCustomer
    {
        void ILocalCustomer.CustomerDetails()
        {
            Console.WriteLine("Local Customer");
        }

        void IGlobalCustomer.CustomerDetails()
        {
            Console.WriteLine("Global Customer");
        }
    }

To be able to call any of these methods from another class, the first thing comes to your mind is to make the methods Public like this:

public class Customer :ILocalCustomer,IGlobalCustomer
    {
        public void ILocalCustomer.CustomerDetails()
        {
            Console.WriteLine("Local Customer from Interface 1");
        }

        public void IGlobalCustomer.CustomerDetails()
        {
            Console.WriteLine("Global Customer from Interface 2");
        }
    }

The Visual Studio IDE immediately underlines it as red and shows error:

 

Now how to access these methods from another class without any access modifier like Public, Protected etc? This involves little trick.

static void Main(string[] args)
        {
            Customer customer = new Customer();

            ILocalCustomer local = (ILocalCustomer)customer;
            local.CustomerDetails();

            IGlobalCustomer global = (IGlobalCustomer)customer;
            global.CustomerDetails();
        }

Here we are casting instance with the Interface type to be able to access the method implementations in class Customer.

Here is the full example:

public interface ILocalCustomer
    {
        void CustomerDetails();
    }

    public interface IGlobalCustomer
    {
        void CustomerDetails();
    }
    class MainClass
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();

            ILocalCustomer local = (ILocalCustomer)customer;
            local.CustomerDetails();

            IGlobalCustomer global = (IGlobalCustomer)customer;
            global.CustomerDetails();
        }
    }

    public class Customer :ILocalCustomer,IGlobalCustomer
    {
        void ILocalCustomer.CustomerDetails()
        {
            Console.WriteLine("Local Customer from Interface 1");
        }

        void IGlobalCustomer.CustomerDetails()
        {
            Console.WriteLine("Global Customer from Interface 2");
        }
    }

 

Share your thoughts.

 

Tags:

.NET | C#

How to call a non-static method from a static method?

by Rohit 7. December 2014 22:12

To call a non-static method from a static method:

  1. Create an instance of the class
  2. Call the non-static method
class MyClass
    {
        static void Main(string[] args)
        {
            Method1();
        }

        static void Method1()
        {
            MyClass p = new MyClass();
            p.Method2();
        }

        void Method2()
        {
            Console.WriteLine("Called from Static Method");
            Console.Read();
        }
    }

Tags: ,

C# | OOP

About Rohit Prakash

Software Craftsman and Technology Enthusiast (not a Guru).

Technical Reviewer of a book on open-source programming IDE.

My day job keeps me engaged with Microsoft Technologies (ASP.NET, C# and SQL Server) hence most of the posts are related to these technologies. I, however, love to play with other open-source technologies. I also have interest in IT Security and therefore you will find posts on Malware Analysis, Cryptography and Anti-Virus programs.

Few people have contacted me for Guest Posts. You will find these posts as well.

 

You can reach me at:

rohit [at] irohitable.com

-----------------------------------------

LinkedIn:

in.linkedin.com/in/rohitpkhare

Twitter:

@BuzzRohit

-----------------------------------------
Google+

Search with this name: Rohit Prakash

Month List

Protected by Copyscape Web Plagiarism Software