Unbreakable
Unbreakable

Reputation: 8132

How to access the property of different concrete classes when interface is passed

I am learning C# and trying to understand how to access different property of classes when interface being passed to the consumer class. Please guide me.

public interface ITest
{
    int ID {get; set;}
}

public class TestA: ITest
{
    public int A {get; set;}
}

public class TestB: ITest
{
    public int B {get; set;}
}


public void Test(ITest test)
{
    // how to check/access property of TestA 
    // how to check/access property of TestB
}

Attempt:

   public void Test(ITest test)
    {
        if(test.GetType() == typeof(TestA))
        {
             test.A = 45678;
        }
    }

Upvotes: 1

Views: 703

Answers (4)

spender
spender

Reputation: 120528

If there's something implementation-specific that you need to do with inheritors of your interface, don't sniff type... write an implementation-specific method to do this for you. Now the caller doesn't need to know anything further about the interface:

void Main()
{
    var tests = new ITest[] { new TestA { A = 1, ID = 0 }, new TestB { B = 10, ID = 1 } };
    foreach (var test in tests)
    {
        test.DoSomeProcessing();
    }
}

public interface ITest
{
    int ID { get; set; }
    void DoSomeProcessing();
}

public class TestA : ITest
{
    public int A { get; set; }
    public int ID { get; set; }
    public void DoSomeProcessing()
    {
        Console.WriteLine("A = " + this.A);
    }
}

public class TestB : ITest
{
    public int B { get; set; }
    public int ID { get; set; }
    public void DoSomeProcessing()
    {
        Console.WriteLine("B = " + this.B);
    }
}

Upvotes: 1

Christian Gollhardt
Christian Gollhardt

Reputation: 17034

First, your test cases don't implement ITest. It needs to be:

public class TestA: ITest
{
    public int A {get; set;}
    public int Id { get; set; }
}

public class TestB: ITest
{
    public int B {get; set;}
    public int Id { get; set; }
}

Then you can check the type via is and cast it or use pattern matching as following:

public void Test(ITest test)
{
    if (test is TestA testA)
    {
        var foo = testA.A;
    }

    if (test is TestB testB)
    {
        var foo = testB.B;
    }
}

Note, above is a C# 7.0 feature. When you can not use the new feature set, you need to cast them as following:

public void Test(ITest test)
{
    var testA = test as TestA;
    var testB = test as TestB;

    if (testA != null)
    {
        var foo = testA.A;
    }

    if (testB != null)
    {
        var foo = testB.B;
    }
}

Upvotes: 1

Hien Nguyen
Hien Nguyen

Reputation: 18973

You can use is keyword to check type and cast to TestA to assign variable

 public void Test(ITest test)
        {
            if(test is TestA)
            {
                 ((TestA)test).A = 45678;
            }
        }

Upvotes: 1

Mureinik
Mureinik

Reputation: 312219

You could explicitly downcast it:

((TestA) test).A = 45678;

Upvotes: 1

Related Questions