8 Replies - 297 Views - Last Post: 14 January 2013 - 09:04 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

problem with interface conversion .. help please

Posted 13 January 2013 - 06:06 PM

summary

I'm trying to retrive a type from the assembly ( the playertype parameter is an enum which has names of the types the user can choose from) the type.tostring is called to store the value of the enum as a string(which is the name of the type in string) this string (playertype name) which holds the type name is passed to the GetAssembly() to return the assembly this type is in. I then call the createinstance() method and pass in the name in the type which returns an object and then cast the object to a IplayerType interface. The method should return an Iplayer interface. all the types in the assembly implements the iplayertype interface.

problem

the result of this code is that its returning a null, when I wanted it to return an Iplayer, as all the type in my assembly implement this interface.

I trying o solve this for hours, would preciate any help.

any ideas?


   public IplayerType GetPlayerType(playerType type)
        {
            string playerTypeName = type.ToString();

            
            return Assembly.GetAssembly(type.GetType()).CreateInstance(playerTypeName) as IplayerType;            
                   

        }



thanks in advance

ray

Is This A Good Question/Topic? 0
  • +

Replies To: problem with interface conversion .. help please

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: problem with interface conversion .. help please

Posted 13 January 2013 - 06:26 PM

First ensure that the CreateInstance() returns an object before using the as operator. If CreateInstance in returning null, then your problem is with the playerTypeName that you are passing in.
Was This Post Helpful? 0
  • +
  • -

#3 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: problem with interface conversion .. help please

Posted 14 January 2013 - 05:31 AM

playername is returning the string with the correct value.

how do I retrive a type from the assembly and cast it into interface type?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: problem with interface conversion .. help please

Posted 14 January 2013 - 05:43 AM

You do as you are doing in your original post. This is why I was asking if CreateInstance() is even returning an object.
Was This Post Helpful? 0
  • +
  • -

#5 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: problem with interface conversion .. help please

Posted 14 January 2013 - 06:00 AM

 

        public IplayerType GetPlayerType(playerType type)
        {
            string playerTypeName = type.ToString();

            
            return Assembly.GetAssembly(playerTypeName.GetType()).CreateInstance(playerTypeName) as IplayerType;            
                   

        }




any ideas on solving this?
Was This Post Helpful? -1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: problem with interface conversion .. help please

Posted 14 January 2013 - 06:22 AM

*sigh* Can you try changing your code to this?

public IplayerType GetPlayerType(playerType type)
{
    string playerTypeName = type.ToString();
    object playerObject = Assembly.GetAssembly(playerTypeName.GetType()).CreateInstance(playerTypeName);
    IplayerType playerType = playerObject as IplayerType;

    if (playerObject == null)
        throw new ApplicationException(String.Format("Could not instantiate {0} object.", playerTypeName));

    if (playerType == null)
        throw new ApplicationException("Could not cast object to IplayerType");

    return playerType;
}



Anyway, if you you get the could not instantiate the object exception, then you have bigger problems than just casting.

If you get the could net cast exception, then you'll have to implement an adapter instead of casting.
Was This Post Helpful? 1
  • +
  • -

#7 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: problem with interface conversion .. help please

Posted 14 January 2013 - 06:51 AM

Thanks for your code skydiver,

I tried your code, but the problem is that the createinstance(playerTypeName); returns null and not the object.


object playerObject = Assembly.GetAssembly(playerTypeName.GetType()).CreateInstance(playerTypeName);




the method parameter (enum playertype type), i checked the values with the names of the types i'm trying to retrieve and no typo errors there. I just checked the type names in the assembly itself, but all good.

baffling :whistling:

This post has been edited by ray1234: 14 January 2013 - 06:52 AM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: problem with interface conversion .. help please

Posted 14 January 2013 - 07:37 AM

Most class names have a namespace ahead of them. Your code above seems to just treat the enum value string as a class name.
Was This Post Helpful? 1
  • +
  • -

#9 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: problem with interface conversion .. help please

Posted 14 January 2013 - 09:04 AM

View PostSkydiver, on 14 January 2013 - 02:37 PM, said:

Most class names have a namespace ahead of them. Your code above seems to just treat the enum value string as a class name.


yippe!! that was it. I spent hours trying to figure it out, and all it was, a simple namespace definition. thanks :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1