开发者

Generic Delegate with the type provided on invoke or function add

开发者 https://www.devze.com 2022-12-08 08:01 出处:网络
What I am going after is to be able to do something like this public class Class1 { public delegate CollsionType ValidateDelegate< T >(PlayerState<T> state, Vector2 position, CollsionType

What I am going after is to be able to do something like this

public class Class1
{
    public delegate CollsionType ValidateDelegate< T >(PlayerState<T> state, Vector2 position, CollsionType boundary);

    public static ValidateDelegate ValidateMove;

    public void AnotherFunction< T >(PlayerState< T > state)
    {
         //option1
         ValidateDelegate.Invoke<T>(state,SomeParameter,SomeParamter2)
    }
}

public class Class2<TypeIWant>
{
    public void SomeFunction
    {
        //option2
        Class1.ValidateMove = new ValidateDelegate<TypeIWant>(functionThatHasCorrectSigniture)
    }
}

The trick here is I d开发者_开发知识库o not know the type T when the delegate is created in Class1. I only know T when a function is added to the delegate or when the delegate is invoked.


You're almost there - just make Class1 generic, and the delegate generic:

public class Class1<T>
{
    public delegate CollsionType ValidateDelegate<T>(PlayerState<T> state, Vector2 position, CollsionType boundary);

    public static ValidateDelegate<T> ValidateMove;

    public void AnotherFunction<T>(PlayerState<T> state)
    {
         ValidateDelegate.Invoke<T>(state,SomeParameter,SomeParamter2)
    }
}

And specify the type parameter twice - once for the class, and once for the delegate:

public class Class2
{
    public void SomeFunction()
    {
        Class1<TypeIWant>.ValidateMove = new ValidateDelegate<TypeIWant>(functionThatHasCorrectSigniture)
    }
}


I solved the problem by making the function I was calling on Class 2 static(erasing the need for the delegate) and passing in the generic types of Class2 to the function in Class1 that was calling the function on Class2.

There could still be more valid answers to this question. This one just works for me for now because I was able to make the function static. if that was not the case I would still be stuck. And I am still interested in answers that don't require the function to be static or Class1 being Generic as this would open up some options for my project.


Here is the closest I could come to the correct answer to my question. I do admit that it goes against some coding standards(because it uses late binding) but it seems to work.

public class Class1

{

public delegate CollsionType ValidateDelegate< T >(PlayerState<T> state, Vector2 position, CollsionType boundary);

public static Object ValidateMove;

public void AnotherFunction< T >(PlayerState< T > state)
{
     ValidateDelegate<T> ValFunction = (ValidateDelegate<T>)ValidateMove; 
     ValFunction.Invoke<T>(state,SomeParameter,SomeParamter2);
}

}

public class Class2

{

public void SomeFunction
{

    Class1.ValidateMove = new ValidateDelegate<TypeIWant>(functionThatHasCorrectSigniture)
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号