سؤال

أحتاج حقًا إلى إنشاء شيء مثل ما يلي ، أقوم ببناء فئتين ، الأول هو فئة تحمل اسم Tablenameassingular (أي العنوان) ، في فصل العمال الثاني ، أحتاج إلى الحصول على شيء مثل ما يلي

public IEnumerable<AddressEntity> GetAddressEntity()
{
 // the good stuff...
}

عند إنشاء الوظيفة لدي ما يلي ..

Type t = Type.GetType("IEnumerable<" + tableNameAsSingular + ">");
CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, t, -1, vsCMAccess.vsCMAccessPublic, null);

لكن تي دائما لاغية

عندما أفعل Type.GetType(tableNameAsSingular) يعود فارغة أيضا

سيتم استلام أي مساعدة أو مؤشرات بشكل كبير. وأيضًا إذا كان أي شخص يعرف مكان وجود عدد كبير من المعرفة في توليد رمز Envdte ، فسأكون رائعًا!


تحديث

لقد جربته الآن كسلسلة باستخدام ما يلي:

   public void AddFinderMethod()
    {
        string t = "IEnumerable<" + tableNameAsSingular + ">";
        CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, t, -1, vsCMAccess.vsCMAccessPublic, null);
        // Code here remove as it does not get this far yet.
    }

لكني أحصل "IEnumerable<ProductEntity> is not a valid identifier"رسالة الخطأ في طريقة AddFunction

هل كانت مفيدة؟

المحلول 2

تمكنت من تشغيلها مع ما يلي:

string returnType = "System.Collections.Generic.IEnumerable<" + tableNameAsSingular + ">"; 
CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, returnType, -1, vsCMAccess.vsCMAccessPublic, null);

نصائح أخرى

بناء الجملة IEnumerable<T> هو C# بناء الجملة ، وليس .NET بناء الجملة (الذي يستخدم العرق الخلفي ، العدادات ، إلخ). ما تعنيه هو:

Type tableType = Type.GetType(assemblyQualifiedNameToEntity);
Type enumerableType = typeof(IEnumerable<T>).MakeGenericType(tableType);

لاحظ أن Assembly.GetType عادة ما يكون اختيارًا أفضل ، حيث يمكنك فقط استخدام أسماء مؤهلة للمساحة:

Assembly asm = typeof(SomeKnownType).Assembly;
Type tableType = asm.GetType(namespaceQualifiedNameToEntity);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top