0

So after implemented Page Object Pattern using this tutorial i have several Pages that derived from BasePageElementMap.

And i want to handle some operation so i have this class:

public class DownloadAttachmentsHandler
    {
        public DownloadAttachmentsHandler(BasePageElementMap basePageElementMap)
        {
            Type type = basePageElementMap.GetType();            
        }
    }

Every Pages that derived from BasePageElementMap have this html elements that locate inside its class that derived from BasePageElementMap and from this Page i have this Map object that contains all my HTML elements that i am using.

public class YahooEmailPage: BasePage<YahooEmailPageElementMap, YahooEmailPageValidator>...

so in case i am call this function like this:

UploadAttachmentsHandler att = new UploadAttachmentsHandler(new YahooEmailPage().Map);

I want to cast this into YahooEmailPage from my DownloadAttachmentsHandler method.

So currently i have this type object, how can i case it into YahooEmailPage ?

user979033
  • 5,430
  • 7
  • 32
  • 50

1 Answers1

1

If I understood correctly, you want the following:

public class DownloadAttachmentsHandler
{
    public static object Cast(object obj, Type t)
    { 
        try
        {
            var param = Expression.Parameter(obj.GetType());
            return Expression.Lambda(Expression.Convert(param, t), param)
                 .Compile().DynamicInvoke(obj);
        }
        catch (TargetInvocationException ex)
        {
             throw ex.InnerException;
        }         
    }


    public DownloadAttachmentsHandler(BasePageElementMap basePageElementMap)
    {
        Type type = basePageElementMap.GetType();
        dynamic foo = Cast(basePageElementMap, type);
    }
}

Based on this answer by balage.

EDIT: For the example, lets assume that GetType() returns the type bar. You will have to create a method like this one:

public static void UseDynamic(bar input)
{
    // Stuff
}

And then do

public DownloadAttachmentsHandler(BasePageElementMap basePageElementMap)
{
    Type type = basePageElementMap.GetType();
    dynamic foo = Cast(basePageElementMap, type);
    UseDynamic(foo);
}

You can use overloads to avoid having to write many ifs or a switch. However, whichever approach you take, you will have to create a method for each possible type.

Community
  • 1
  • 1
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • OK thats fine, i can see that foo is the type i expected but how to see all my functions inside this? how to auto cast it to the correct type ? – user979033 May 10 '17 at 14:01
  • You will have to call another method that takes an argument of the expected type, and pass `foo` to it. – stelioslogothetis May 10 '17 at 14:03
  • I am sorry but i did not understand how to implement it, can you sow me please short example ? – user979033 May 10 '17 at 14:05
  • Thats mean that is i have 100 different types that derived i need 100 methods ? – user979033 May 10 '17 at 17:35
  • Not if they all inherit from 2 or 3 types and the methods of said types are the only ones you need. – stelioslogothetis May 10 '17 at 17:36
  • They all inherit from this BasePageElementMap class, but you said "whichever approach you take, you will have to create a method for each possible type", i have YahooEmailPage, GmailEmailPage and several more (as i mention they all inherit from BasePageElementMap ) – user979033 May 10 '17 at 17:42
  • If you need to use any methods that BasePageElementMap doesnt have, you will have to create a separate method. If not, then there is no need to do the cast in the first place. – stelioslogothetis May 10 '17 at 17:43
  • unfortunately i need to use methods that BasePageElementMap doesnt have but only in the son class so the only way is to create simple switch\if ? – user979033 May 10 '17 at 17:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143901/discussion-between-sty-and-user979033). – stelioslogothetis May 10 '17 at 18:00