简体   繁体   中英

Return Different Types Within One Function C#

I am creating a project data pipeline and I need to return different types from a single class

in this class I have a number of Dictionaries that hold and separate the elements/content i want to load up but i need a way to return them with a single string... as i am not to familiar with these Type functions i am lost as to how to return the content properly

I need this in a separate class so i can do a XML serialization later

Here is what I have now

DataClass contents;
public T ReturnType<T>(string asset)
{
    if(typeof(T) == typeof(int))
    {
        return contents.Integers[Asset];
    }
    if(typeof(T) == typeof(float))
    {
        return contents.Floats[Asset];
    }
    if(typeof(T) == typeof(double))
    {
        return contents.Doubles[Asset];
    }
    return default(T);
}

it will allow me to use a base Object class to parse the content but i dont want anything to get lost in transit so i am weary in using this method

my question is how to return one of the different objects of a certain types within the class that i am using for serialization with a function like that

If i wanted to use the previous function to grab content within the class eg

public Object someobject;
//button event handler to change the current object
//preferably this would be changed depending on the object i would be calling
//but this should do for showing how it is supposed to work
public void ChangeCurrentObject(event e)
{
    someobject = (Object)ReturnType<Object>("23rdObject");
}

it sends a string to the function called 'ReturnType' and returns an object ie(int, float,etc) within there own respective dictionary

The generics in this case will only help you not to write diferent method for every asset type. You can also use this aproach to make it more modular.

     static  class Assets
    {
        public interface IAssetHandler<out T> 
        {
            T GetAsset(string name);
        }

        private static readonly Dictionary<Type,object> _handlers=new Dictionary<Type, object>();

        public static T GetAsset<T>(string name)
        {

            object assetHandler;

            if(!_handlers.TryGetValue(typeof(T),out assetHandler))
            {
              throw  new Exception("No handler for that type of asset");
            }

            return (assetHandler as IAssetHandler<T>).GetAsset(name);

        }


        public static void RegisterAssetHandler<T>(IAssetHandler<T> handler)
        {
            _handlers[typeof (T)] = handler;
        }

    }


 public class IntAssetsHandler:Assets.IAssetHandler<int>
        {
            #region Implementation of IAssetHandler<out int>

            public int GetAsset(string name)
            {
                return 0;
            }

            #endregion
        }





        static void Main(string[] args)
        {
            Assets.RegisterAssetHandler(new IntAssetsHandler());

            Console.WriteLine(Assets.GetAsset<int>("test"));


        }

You could use external class, set the properties types as you wish, then use it in your function.

public class MultipleOpjects
{
    public List<string> ObjectOne { get; set; }

    public List<object> ObjectTwo { get; set; }

    public object ObjectThree { get; set; }

}
public MultipleOpjects GetAnything()
{
    MultipleOpjects Vrble = new MultipleOpjects();
    Vrble.ObjectOne  = SomeThing1;
    Vrble.ObjectTwo = SomeThing2;
    Vrble.ObjectThree = SomeThing3;

    return Vrble;      
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM