vendredi 10 septembre 2021

How to put function as parameter correctly

I can write these methods(onconnected, ondisc, onlogon, onlogof) on the MainClass class (there will be no problems, I will just send the name of the function to the parameter), but it will not be convenient to manage this file (MainClass). Because of this, I wrote these functions in a separate file (CallbackMethodsManager). But now, I can’t send a function from CallbackMethodsManager to MainClass using reflection (no other option came to mind)

When i run the app i getting an error: System.ArgumentException: 'Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.'

:at:

Action<TcallBack> action = Delegate.CreateDelegate(typeof(Action<TcallBack>), T, method) as Action<TcallBack>;

:if I understood correctly then the problem is in the typeof parameter. Please help because of why i getting error, how to correctly construct ReflectCallbackMethod() method, thanks.

ICallbackMethods.cs:

namespace SteamWhoDeletedMe.Interfaces
{
    interface ICallbackMethods
    {
        void OnConnected(SteamClient.ConnectedCallback callback, User user);
        void OnDisconnected(SteamClient.DisconnectedCallback callback, User user);
 
        void OnLoggedOn(SteamUser.LoggedOnCallback callback, User user);
        void OnLoggedOff(SteamUser.LoggedOffCallback callback);
    }
}

CallbackMethodsManager.cs:

namespace SteamWhoDeletedMe.Managers
{
    class CallbackMethodsManager : ICallbackMethods
    {
        public void OnConnected(SteamClient.ConnectedCallback callback, User user)
        {
            Console.WriteLine($"Connected to Steam! Logging in '{user.UserSteamLogin}'...");
 
            user.steamUser.LogOn(new SteamUser.LogOnDetails
            {
                Username = user.UserSteamLogin,
                Password = user.UserSteamPassword,
            });
        }
 
        public void OnDisconnected(SteamClient.DisconnectedCallback callback, User user)
        {
            Console.WriteLine("Disconnected from Steam");
 
            user.isRunning = false;
        }
 
        public void OnLoggedOff(SteamUser.LoggedOffCallback callback)
        {
            Console.WriteLine("Logged off of Steam: {0}", callback.Result);
        }
 
        public void OnLoggedOn(SteamUser.LoggedOnCallback callback, User user)
        {
            if (callback.Result != EResult.OK)
            {
                // if we recieve AccountLogonDenied or one of it's flavors (AccountLogonDeniedNoMailSent, etc) then the account we're logging into is SteamGuard protected
                if (callback.Result == EResult.AccountLogonDenied)
                {
                    // handle steamguard
 
                    Console.WriteLine("Unable to logon to Steam: This account is SteamGuard protected.");
 
                    user.isRunning = false;
                    return;
                }
 
                Console.WriteLine($"Unable to logon to Steam: [{callback.Result}] / [{callback.ExtendedResult}]");
 
                user.isRunning = false;
                return;
            }
 
            Console.WriteLine("Successfully logged on!");
        }
    }
}

MainClass.cs:

// steam user activities
var onCon = Utils<MainClass>.ReflectCallbackMethod<SteamClient.ConnectedCallback>(MC, "OnConnected");
var onDiscon = Utils<MainClass>.ReflectCallbackMethod<SteamClient.DisconnectedCallback>(MC, "OnDisconnected");
var onLogOn = Utils<MainClass>.ReflectCallbackMethod<SteamUser.LoggedOnCallback>(MC, "OnLoggedOn");
var onLogOff = Utils<MainClass>.ReflectCallbackMethod<SteamUser.LoggedOffCallback>(MC, "OnLoggedOff");
     
// register a few callbacks we're interested in these are registered upon creation to a callback manager, which will then route the callbacks to the functions specified
manager.Subscribe<SteamClient.ConnectedCallback>(onCon);
manager.Subscribe<SteamClient.DisconnectedCallback>(onDiscon);
manager.Subscribe<SteamUser.LoggedOnCallback>(onLogOn);
manager.Subscribe<SteamUser.LoggedOffCallback>(onLogOff);

screen: Screen





Aucun commentaire:

Enregistrer un commentaire