سؤال

لقد كتبت طريقة لاستخراج الحقول من كائن مثل هذا:

private static string GetHTMLStatic(ref Object objectX, ref List<string> ExludeFields)
{
    Type objectType = objectX.GetType();
    FieldInfo[] fieldInfo = objectType.GetFields();

    foreach (FieldInfo field in fieldInfo)
    {
        if(!ExludeFields.Contains(field.Name))
        {
            DisplayOutput += GetHTMLAttributes(field);
        }                
    }

    return DisplayOutput;
}

يحتوي كل حقل في صفي أيضًا على سماته الخاصة، وفي هذه الحالة تسمى السمة الخاصة بي HTMLAttributes.داخل حلقة foreach أحاول الحصول على السمات الخاصة بكل حقل والقيم الخاصة به.يبدو حاليا مثل هذا:

private static string GetHTMLAttributes(FieldInfo field)
{
    string AttributeOutput = string.Empty;

    HTMLAttributes[] htmlAttributes = field.GetCustomAttributes(typeof(HTMLAttributes), false);

    foreach (HTMLAttributes fa in htmlAttributes)
    {
        //Do stuff with the field's attributes here.
    }

    return AttributeOutput;
}

تبدو فئة السمات الخاصة بي كما يلي:

[AttributeUsage(AttributeTargets.Field,
                AllowMultiple = true)]
public class HTMLAttributes : System.Attribute
{
    public string fieldType;
    public string inputType;

    public HTMLAttributes(string fType, string iType)
    {
        fieldType = fType.ToString();
        inputType = iType.ToString();
    }
}

يبدو هذا منطقيًا ولكنه لن يتم تجميعه، لدي خط أحمر متعرج في طريقة GetHTMLATtributes() ضمن:

field.GetCustomAttributes(typeof(HTMLAttributes), false);

الحقل الذي أحاول استخراج السمات منه موجود في فئة أخرى تستخدم على النحو التالي:

[HTMLAttributes("input", "text")]
public string CustomerName;

من وجهة نظري (أو عدم وجوده) يجب أن يعمل هذا؟يرجى توسيع رأيي زملائي المطورين!

*تحرير، خطأ مترجم:

لا يمكن تحويل نوع "كائن [] ضمنيًا إلى "data.htmlattributes [].يوجد تحويل صريح (هل تفتقد ممثلين؟)

لقد حاولت صبها مثل هذا:

(HTMLAttributes)field.GetCustomAttributes(typeof(HTMLAttributes), false);

ولكن هذا أيضًا لا يعمل، أحصل على خطأ المترجم هذا:

لا يمكن تحويل النوع "كائن [] إلى "data.htmlattributes"

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

المحلول

GetCustomAttributes ترجع الطريقة object[], ، لا HTMLAttributes[].سبب عودتها object[] هو أنه كان موجودًا منذ الإصدار 1.0، قبل أن ترى الأدوية العامة لـ .NET النور.

يجب عليك إرسال كل عنصر يدويًا في قيمة الإرجاع إلى HTMLAttributes.

لإصلاح الكود الخاص بك، ما عليك سوى تغيير السطر إلى:

object[] htmlAttributes = field.GetCustomAttributes(typeof(HTMLAttributes), false);

foreach سوف يعتني بالممثلين نيابةً عنك.

تحديث:

أنت لا ينبغي يلقي المصفوفة التي تم إرجاعها إلى HTMLAttributes[].قيمة الإرجاع ليست كذلك HTMLAttributes[].انه object[] تحتوي على عناصر من النوع HTMLAttributes.إذا كنت تريد أ HTMLAttribute[] الكائن المكتوب (الذي لا تحتاجه في مقتطف التعليمات البرمجية المحدد هذا، foreach سيكون كافيا)، يجب عليك إرسال كل عنصر من عناصر المصفوفة بشكل فردي إلى HTMLAttribute;ربما باستخدام LINQ:

HTMLAttributes[] htmlAttributes = returnValue.Cast<HTMLAttributes>().ToArray();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top