using the Dynamic keyword in Unity possible?

When i try to run my Dictionary of dynamic lists Unity throws this error allthough no errors are shown in visual studio:

“Dynamic keyword requires `System.Runtime.CompilerServices.DynamicAttribute’ to be defined. Are you missing System.Core.dll assembly reference?”

My code(no errors here):

   private static Dictionary<EffectTypes, List<dynamic>> rewardItemsOwned = new Dictionary<EffectTypes, List<dynamic>>(); 

    private void TestPutToInventory()
    {
        rewardItemsOwned.Add(EffectTypes.DeathSoundEffect, new List<DeathSoundEffect>() as dynamic);
        rewardItemsOwned[EffectTypes.DeathVisualEffect].Add(new DeathSoundEffect("LosenedBowels", RewardAssetContainer.LosenedBowels, 0.5f, EffectTypes.DeathSoundEffect, Rareity.Rare, RewardAssetContainer.EpicExample));
    }

    public static dynamic GetSpecificItem(EffectTypes _type, string _itemName)
    {
        foreach (var item in rewardItemsOwned[EffectTypes.DeathVisualEffect])
        {
            if (item.GetName() == _itemName)
            {
                return rewardItemsOwned[item];
            }
        }
        return null;
    }

And then in a another method i call the GetSpecificItem like so also with no errors:

 var temp = InventoryManager.GetSpecificItem(EffectTypes.DeathSoundEffect, "LosenedBowels");

Unity uses it’s own version of Mono, and it’s pretty old. If you’re using Unity 2017 or later, you could try turning on the experimental runtime upgrade (Edit-Project Settings->Player->Other Settings->Configuration), by switching to .NET 4.6. It might be that dynamic works there.

Unfortunatly we have no switched to 2017 at the office yet. However i might try this out in a test project, in case it works i might be able to persuade my colleagues to upgrade :wink:

If you’re only developing for PC you can use C# 6.0 features with the old Unity by adding the incremental compiler (GitHub - SaladLab/Unity3D.IncrementalCompiler: Unity3D Incremental C# Compiler using Roslyn) - I used it a while back and it worked well (and was fast)

Just sight-reading, but it looks like the old standard C# way should work: generics and an interface like IGetName. Something like: class SpecialDict where T : IGetName

(Disclaimer: Can barely use them. I think of C# generics and dynamic as “a funny way to implement C++ templates” since I learned those first.)