0

Im trying to pass Type and reference to a generic via a string of the same name:

IE: string ButtonName == "ActionOne" ~~~~~>> Type == ActionOne, reference == ActionOne

Sadly I have no idea what the right syntax for this is, and am having a hard time finding it via google, so if someone knows how to do this, or at-least what it is called, it would help a huge amount!!

Thanks, and here is some sudo code of what i'm trying to do!

public class ActionPanelButton : *NotImportant* {
    private string ButtonName;
    private Type ButtonType; 

    void ActionPanelButton(){
            ButtonName = this.Button.name; // This is return a string set by the user
            ButtonType = Type.GetType(ButtonName);
            ActionPanel = this.Button.parent.ActionPanel;  //  This will return the containing panel object 
    }   

    public void LeftClick(){ //This responds like any-ol OnClick

        ActionPanel.ChangeSelectedActionBundle<(ButtonType)>(ActionPanel.DisplayedActionPanel.(ButtonType));
    }
}

public class ActionPanel....

public void ChangeSelectedActionBundle <T> (T action){

        Type typeOfObject = typeof(ActionBundle);
        Type typeOfAction = typeof(T);

        FieldInfo field = typeOfObject.GetField(typeOfAction.Name);
        field.SetValue(SelectionManager.SelectedActionBundle, action);
    }
5
  • Since generics work through the compiler, the type needs to be known at compile time. There's not really an easy way to do what you're doing, short of using reflection. Commented Sep 24, 2013 at 22:20
  • I'm having a hard time understanding what you're after. Can you perhaps change the example? It does appear though that Reflection is more what you're after.. not generics. Commented Sep 24, 2013 at 22:20
  • As soon as I get home, I will expand my context!! Thanks for trying to help! Commented Sep 24, 2013 at 22:23
  • 2
    @Burdock - They want to call a generic method based on a type that's only known at runtime. Commented Sep 24, 2013 at 22:24
  • @MikeChristensen has it right~ Let me check my code and I will update the post... Commented Sep 24, 2013 at 22:37

1 Answer 1

1

Generic functions are actually more a compiler feature than a runtime feature. When the compiler runs into code such as:

myObj.Foo<Int32>(123);

It will create an implementation of Foo using an Int32 instead of T. For this reason, you cannot call a generic function using a type that's only known as runtime.

However, I question why you need to do this. You're already using reflection in your ChangeSelectedActionBundle method. Nothing about it needs to be generic. You can just do:

public void ChangeSelectedActionBundle(object action)
{
   Type typeOfObject = typeof(ActionBundle);
   Type typeOfAction = action.GetType();

   FieldInfo field = typeOfObject.GetField(typeOfAction.Name);
   field.SetValue(SelectionManager.SelectedActionBundle, action);
}

Perhaps rather than using object, you could use a base class common to all your actions, use or an interface.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.