diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-08-01 13:11:51 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-08-01 13:11:51 -0400 |
commit | 5b31be76dd90711ff475341de8dfdb6e1f50b98a (patch) | |
tree | e120bc62af72e94e8e924ba124382d0c6f9304de /src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs | |
parent | 8f96a97f070d654764de3b138678d8f62707f485 (diff) | |
parent | d688cdf8c3c852d4b11cdd046d67c4b35443cc95 (diff) | |
download | SMAPI-5b31be76dd90711ff475341de8dfdb6e1f50b98a.tar.gz SMAPI-5b31be76dd90711ff475341de8dfdb6e1f50b98a.tar.bz2 SMAPI-5b31be76dd90711ff475341de8dfdb6e1f50b98a.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs')
-rw-r--r-- | src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs b/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs index 464367b6..8d1b6034 100644 --- a/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs +++ b/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs @@ -36,23 +36,26 @@ namespace StardewModdingAPI.Framework.Reflection public TInterface CreateProxy<TInterface>(object instance, string sourceModID, string targetModID) where TInterface : class { - // validate - if (instance == null) - throw new InvalidOperationException("Can't proxy access to a null API."); - if (!typeof(TInterface).IsInterface) - throw new InvalidOperationException("The proxy type must be an interface, not a class."); - - // get proxy type - Type targetType = instance.GetType(); - string proxyTypeName = $"StardewModdingAPI.Proxies.From<{sourceModID}_{typeof(TInterface).FullName}>_To<{targetModID}_{targetType.FullName}>"; - if (!this.Builders.TryGetValue(proxyTypeName, out InterfaceProxyBuilder builder)) + lock (this.Builders) { - builder = new InterfaceProxyBuilder(proxyTypeName, this.ModuleBuilder, typeof(TInterface), targetType); - this.Builders[proxyTypeName] = builder; - } + // validate + if (instance == null) + throw new InvalidOperationException("Can't proxy access to a null API."); + if (!typeof(TInterface).IsInterface) + throw new InvalidOperationException("The proxy type must be an interface, not a class."); - // create instance - return (TInterface)builder.CreateInstance(instance); + // get proxy type + Type targetType = instance.GetType(); + string proxyTypeName = $"StardewModdingAPI.Proxies.From<{sourceModID}_{typeof(TInterface).FullName}>_To<{targetModID}_{targetType.FullName}>"; + if (!this.Builders.TryGetValue(proxyTypeName, out InterfaceProxyBuilder builder)) + { + builder = new InterfaceProxyBuilder(proxyTypeName, this.ModuleBuilder, typeof(TInterface), targetType); + this.Builders[proxyTypeName] = builder; + } + + // create instance + return (TInterface)builder.CreateInstance(instance); + } } } } |