{"record":{"id":"08ffde7d09b34f63","repo":"EllanJiang/GameFramework","slug":"you-must-get-module-by-interface-but-0-is-not","errorCode":null,"errorMessage":"You must get module by interface, but '{0}' is not.","messagePattern":"You must get module by interface, but '(.+?)' is not\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Base/GameFrameworkEntry.cs","lineNumber":60,"sourceCode":"\n            s_GameFrameworkModules.Clear();\n            ReferencePool.ClearAll();\n            Utility.Marshal.FreeCachedHGlobal();\n            GameFrameworkLog.SetLogHelper(null);\n        }\n\n        /// <summary>\n        /// 获取游戏框架模块。\n        /// </summary>\n        /// <typeparam name=\"T\">要获取的游戏框架模块类型。</typeparam>\n        /// <returns>要获取的游戏框架模块。</returns>\n        /// <remarks>如果要获取的游戏框架模块不存在，则自动创建该游戏框架模块。</remarks>\n        public static T GetModule<T>() where T : class\n        {\n            Type interfaceType = typeof(T);\n            if (!interfaceType.IsInterface)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"You must get module by interface, but '{0}' is not.\", interfaceType.FullName));\n            }\n\n            if (!interfaceType.FullName.StartsWith(\"GameFramework.\", StringComparison.Ordinal))\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"You must get a Game Framework module, but '{0}' is not.\", interfaceType.FullName));\n            }\n\n            string moduleName = Utility.Text.Format(\"{0}.{1}\", interfaceType.Namespace, interfaceType.Name.Substring(1));\n            Type moduleType = Type.GetType(moduleName);\n            if (moduleType == null)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Can not find Game Framework module type '{0}'.\", moduleName));\n            }\n\n            return GetModule(moduleType) as T;\n        }\n\n        /// <summary>","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Base/GameFrameworkEntry.cs#L42-L78","documentation":"GameFrameworkEntry.GetModule<T> requires T to be an interface so it can locate the concrete implementation class by naming convention (IWebRequestAgent -> WebRequestAgent, etc.). Passing a class or struct as T breaks that lookup, so it throws GameFrameworkException immediately.","triggerScenarios":"Calling GetModule<SomeClass>() where typeof(T).IsInterface is false — e.g. GetModule<WebRequest>() or GetModule<DefaultLogHelper>() instead of the corresponding interface.","commonSituations":"IDE auto-import picking the concrete type instead of the GameFramework interface; refactoring that merged an interface into a class; new developers requesting a helper class directly rather than its interface; copying a GetModule call and editing the generic argument to a non-module type.","solutions":["Pass the interface type instead, e.g. GetModule<IWebRequestAgent>-style: GetModule<IEvent>() not GetModule<Event>().","Define an interface for your custom module if it doesn't have one, and request that interface.","Check the using directives — a same-named class from another namespace may be resolved instead of the interface.","Verify the type argument is one of the GameFramework module interfaces (IEvent, IUI, IResource, etc.)."],"exampleFix":"// before\nvar eventComponent = GameFrameworkEntry.GetModule<Event>();\n// after\nvar eventComponent = GameFrameworkEntry.GetModule<IEvent>();","handlingStrategy":"type-guard","validationCode":"var t = typeof(T); if (!t.IsInterface) throw new ArgumentException(\"GetModule<T> requires an interface type: \" + t.FullName);","typeGuard":"static bool IsModuleInterface<T>() where T : class => typeof(T).IsInterface && typeof(T).FullName.StartsWith(\"GameFramework.\", StringComparison.Ordinal);","tryCatchPattern":"try { var mod = GameFrameworkEntry.GetModule<T>(); } catch (GameFrameworkException ex) { Log.Fatal(\"GetModule requires an interface: {0}\", ex.Message); throw; }","preventionTips":["Always request modules via their I-prefixed interfaces (IEvent, IUI, IResource)","Check the resolved type in IntelliSense — a same-named class may shadow the interface","Create an interface for custom modules before registering them"],"tags":["gameframework-entry","getmodule","generics","interface","csharp"],"backgroundTag":"type-mismatch","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}