bflemi3
bflemi3

Reputation: 6790

Get base class from inheriting class

I would like to get the base class from

public class Class1 : BrowserWindow

I am trying to "convert" Class1 into UiBrowserWindow via a method in UiBrowserWindow. Something like this...

public class UiBrowserWindow : Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow {
    public static UiBrowserWindow Convert(Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow browserWindow) {
        UiBrowserWindow result = new UiBrowserWindow();
        result = (UiBrowserWindow)browserWindow;
        return result;
    }
}

UiBrowserWindow browserWindow = UiBrowserWindow.Convert(UIMap.Class1.GetType().BaseType);
//UIMap.Class1 is a property of UIMap

This code produces the error Argument type 'System.Type' is not assignable to parameter type 'Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow'

UPDATED CODE:

public class UiBrowserWindow : Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow {
    public static UiBrowserWindow convert(Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow browserWindow) {
        UiBrowserWindow result = (UiBrowserWindow)browserWindow;
        return result;
    }
}

// Usage...
UiBrowserWindow browserWindow = UiBrowserWindow.convert(UIMap.Class1);

This is causing an exception InvalidCastException: Unable to cast object of type 'automatedTesting.Class1' to type 'UiBrowserWindow'

TestMethod...

[TestMethod]
public void CodedUITestMethod1() {
    this.UIMap.RecordedMethod1();

    UiBrowserWindow browserWindow = UiBrowserWindow.convert(UIMap.Class1);
    Assert.IsNotNull(browserWindow.getUiTestControl<HtmlDiv>(new[] { 
        new PropertyExpression(HtmlDiv.PropertyNames.Id, "logo")
    }));
}

Upvotes: 0

Views: 978

Answers (1)

Novakov
Novakov

Reputation: 3095

I suppose that you should omit ".GetType().BaseType" part and call:

UiBrowserWindow browserWindow = UiBrowserWindow.Convert(UIMap.Class1);

Update: Class1 derives from BrowserWindow, UiBrowserWindow derives from BrowserWindow. Class1 is NOT UiBrowserWindow. The conversion you're trying will work only with overload cast operator (but I don't recommend it). Explain what you're trying to achive because this way will not work

Upvotes: 2

Related Questions