{"id":292,"date":"2015-12-16T21:56:47","date_gmt":"2015-12-16T17:56:47","guid":{"rendered":"http:\/\/infobs.athn.ru\/?p=292"},"modified":"2015-12-16T21:56:47","modified_gmt":"2015-12-16T17:56:47","slug":"ef-i-generic","status":"publish","type":"post","link":"https:\/\/infobase.athn.ru\/program\/2015\/12\/ef-i-generic\/","title":{"rendered":"EF \u0438 Generic"},"content":{"rendered":"<p>\u041e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u043a\u043e\u0434\u0430 \u0441\u0435\u0431\u0435 \u043d\u0430 \u043f\u0430\u043c\u044f\u0442\u044c. \u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439, \u043d\u043e \u0432 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u043e\u0441\u0442\u044b\u043b\u0435\u0439.<\/p>\n<pre>#region Generic\n        public void CreateLang(TModel langModel, out IEnumerable diff)\n        {\n            Type entityType = this.TypesModelEntity[typeof(TModel)]; \/\/\u0432\u043e\u0442 \u043a\u043e\u0441\u0442\u044b\u043b\u044c\n            var langType = this.TypesEntityLang[entityType]; \/\/\u0438 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d\n\n            Type genericClass = typeof(EFLangRepository);\n            Type constructedClass = genericClass.MakeGenericType(typeof(TModel), entityType, langType);\n\n            object repo = Activator.CreateInstance(constructedClass);\n\n            diff = new List();\n            object[] args = new object[] { langModel, diff };\n\n            MethodInfo magicMethod = constructedClass.GetMethod(\"CreateLang\");\n            object magicValue = magicMethod.Invoke(repo, args);\n            diff = args[1] as List;\n        }\n        #endregion\n<\/pre>\n<p><!--more--><br \/>\n\u041f\u0440\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/266115\/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class\">http:\/\/stackoverflow.com\/questions\/266115\/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class<\/a><\/p>\n<pre>using System;\nusing System.Reflection;\n\npublic class Generic\n{\n    public Generic()\n    {\n        Console.WriteLine(\"T={0}\", typeof(T));\n    }\n}\n\nclass Test\n{\n    static void Main()\n    {\n        string typeName = \"System.String\";\n        Type typeArgument = Type.GetType(typeName);\n\n        Type genericClass = typeof(Generic);\n        \/\/ MakeGenericType is badly named\n        Type constructedClass = genericClass.MakeGenericType(typeArgument);\n\n        object created = Activator.CreateInstance(constructedClass);\n    }\n}<\/pre>\n<p>Note: if your generic class accepts multiple types, you must include the commas when you omit the type names, for example:<\/p>\n<pre>Type genericClass = typeof(IReadOnlyDictionary);\nType constructedClass = genericClass.MakeGenericType(typeArgument1, typeArgument2);<\/pre>\n<p>\u0418 MSDN<\/p>\n<p><a href=\"https:\/\/msdn.microsoft.com\/ru-ru\/library\/a89hcwhh(v=vs.110).aspx\">https:\/\/msdn.microsoft.com\/ru-ru\/library\/a89hcwhh(v=vs.110).aspx<\/a><\/p>\n<pre>public class MagicClass\n{\n    private int magicBaseValue;\n\n    public MagicClass()\n    {\n        magicBaseValue = 9;\n    }\n\n    public int ItsMagic(int preMagic)\n    {\n        return preMagic * magicBaseValue;\n    }\n}\n\npublic class TestMethodInfo\n{\n    public static void Main()\n    {\n        \/\/ Get the constructor and create an instance of MagicClass\n\n        Type magicType = Type.GetType(\"MagicClass\");\n        ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);\n        object magicClassObject = magicConstructor.Invoke(new object[]{});\n\n        \/\/ Get the ItsMagic method and invoke with a parameter value of 100\n\n        MethodInfo magicMethod = magicType.GetMethod(\"ItsMagic\");\n        object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});\n\n        Console.WriteLine(\"MethodInfo.Invoke() Example\\n\");\n        Console.WriteLine(\"MagicClass.ItsMagic() returned: {0}\", magicValue);\n    }\n}<\/pre>\n<p>\u0412\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/232535\/how-do-i-use-reflection-to-call-a-generic-method\">http:\/\/stackoverflow.com\/questions\/232535\/how-do-i-use-reflection-to-call-a-generic-method<\/a><\/p>\n<pre>MethodInfo method = typeof(Sample).GetMethod(\"GenericMethod\");\nMethodInfo generic = method.MakeGenericMethod(myType);\ngeneric.Invoke(this, null);<\/pre>\n<p>\u041f\u0440\u043e generic-\u043c\u0435\u0442\u043e\u0434<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/2247553\/how-to-pass-variable-of-type-type-to-generic-parameter\">http:\/\/stackoverflow.com\/questions\/2247553\/how-to-pass-variable-of-type-type-to-generic-parameter<\/a><\/p>\n<pre>var method =\n    typeof(MetaDataUtil)\n    .GetMethod(\"GetColumnasGrid\")\n    .MakeGenericMethod(new [] { type })\n    .Invoke(null, null);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u041e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u043a\u043e\u0434\u0430 \u0441\u0435\u0431\u0435 \u043d\u0430 \u043f\u0430\u043c\u044f\u0442\u044c. \u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439, \u043d\u043e \u0432 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u043e\u0441\u0442\u044b\u043b\u0435\u0439. #region Generic public void CreateLang(TModel langModel, out IEnumerable diff) { Type entityType = this.TypesModelEntity[typeof(TModel)]; \/\/\u0432\u043e\u0442 \u043a\u043e\u0441\u0442\u044b\u043b\u044c var <a class=\"more-link\" href=\"https:\/\/infobase.athn.ru\/program\/2015\/12\/ef-i-generic\/\">Continue Reading \u2192<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,20],"tags":[81,89,90],"class_list":["post-292","post","type-post","status-publish","format-standard","hentry","category-csharp","category-mycode","tag-generic","tag-instance","tag-invoke"],"_links":{"self":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":0,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"wp:attachment":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}