#lets make a thread

1 messages · Page 1 of 1 (latest)

neon crown
#

Hi

sour raptor
#

yo

neon crown
#

ok, so this is a solution
let me just pull up some code

#

So I have this
public static Dictionary<Destination, List<IInjectable>> injected = new Dictionary<Destination, List<IInjectable>>();

#

and this
using System;

namespace SSSoftware.Network.Shared
{
public interface IInjectable
{
Destination destination
{
get;
}

    EntryPoint[] EntryPoints
    {
        get;
    }

    object Execute(EntryPoint entryPoint, object obj);
}

}

#

now here any class can register a Destination (what it is) and multiple Entrypoints (what you want to do)

sour raptor
#

Destination is the class object right?

neon crown
#

I use an Enum but it can be anything you can recognise

#

like wise EntryPoint is an Enum

#

so an implementation of this is like

    public class Inject : IInjectable
    {
        public Inject()
        {
            if (Common.ErrMsg != null)
            Common.ErrMsg($"{destination}: Injecting", false);
        }

        public Destination destination
        {
            get
            {
                return Destination.Echo;
            }
        }

        public EntryPoint[] EntryPoints
        {
            get
            {
                return new EntryPoint[] { EntryPoint.Init, EntryPoint.Open, EntryPoint.Send, EntryPoint.Receive, EntryPoint.Close };
            }
        }

        public object Execute(EntryPoint entryPoint, object obj)
        {
            switch (entryPoint)
            {
                case EntryPoint.Init:
                {
                    ControlData controlData = (ControlData)obj;
                    Init(controlData);
                    break;
                }
                case EntryPoint.Open:
                {
                    PortData portData = (PortData)obj;
                    return Open(portData);
                }
                case EntryPoint.Send:
                {
                    Send send = (Send)obj;
                    return Send(send);
                }
                case EntryPoint.Receive:
                {
                    byte[] bytes = (byte[])obj;
                    return Recieve(bytes);
                }
                case EntryPoint.Close:
                {
                    Close();
                    break;
                }
            }
            return null;
        }

#

it's basically like my own Invoke system

sour raptor
#

oh, so basically depending by which value entrypoint is being set to then it changes the methods to call

neon crown
#

exactly, and, if you want to serialize the dependencies then you only need to serialize 2 enums, Destination and EntryPoint

sour raptor
#

ok but what is Destination, another enum?

neon crown
#

yes, I have about 30 different applications that use this system and the Destination is the application

sour raptor
#

right

#

seems an easy to implement workaround

#

I'll let you know

#

Thanks in advance

neon crown
#

very easy and it works brilliantly

#

I use it mainly for client/server network stuff so I can have dynamic dll's which is something c# does not have by default

glad marsh
neon crown
#

and it works perfectly with Unity which many solutions do not

sour raptor
#

How many years of experience of C# do you have?

neon crown
#

I've been programming c# since c# was first introduced

sour raptor
#

woah

#

how old are you then

#

when was it introduced

neon crown
#

very much the wrong side of 65

glad marsh
neon crown
sour raptor
neon crown
#

65 going on 70

sour raptor
#

wow!

neon crown
#

I've been a professional dev for almost 50 years now

glad marsh
neon crown
#

dev is what I do, so why not use the methods available

sour raptor
sour raptor
glad marsh
neon crown
#

I try to teach people the right way to do stuff, but very few people ever listen, they just want quick and easy rather than correct

neon crown
sour raptor
#

this thread deserves to be pinned somewhere

neon crown
#

nah, code is code, nothing else matters

sour raptor
#

coming back to the question, what if I want to perform stuff dynamically in the Execute method? Getting the reference of a Quest and then setting its on complete callback

#

I should still use reflection, since I should get the reference of the methods to being able to call them

#

On the script that you've shown me you're using a switch to then call the methods that you like, but what about many MonoBehaviours that can create Quests and so assign an individual callback to each one of them?

#

@neon crown (since as I'm seeing on the right side he left the thread)

neon crown
#

Not necessary, you can just add a delegate which is executed in the switch, the point is that the calling program does not have to know anything about what is being called except a Destination and an EntryPoint

sour raptor
#

oh right