C# 我应该选择反思还是代表(C)?

C# 我应该选择反思还是代表(C)?,c#,reflection,C#,Reflection,在中,我解释了一个包含表单字段对象的表单类,该类使用配置文件提供程序将数据保存在用户配置文件对象中 代码如下。基本上,我想完成的是将概要文件对象的字段作为参数传递给我的表单字段对象,它们应该进行交互,以便稍后保存数据 您可以在以下行中看到: //LastNameFormLine is an control that was added to my form page. //The ProfileField parameter stores the field of the UserProfile

在中,我解释了一个包含表单字段对象的表单类,该类使用配置文件提供程序将数据保存在用户配置文件对象中

代码如下。基本上,我想完成的是将概要文件对象的字段作为参数传递给我的表单字段对象,它们应该进行交互,以便稍后保存数据

您可以在以下行中看到:

//LastNameFormLine is an control that was added to my form page.
//The ProfileField parameter stores the field of the UserProfile object that is being manipulated by this control
LastNameFormLine.ProfileField = "UserProfile.LastName";
我阅读了关于反射的文章,以便能够在UserProfileVisitor类中保存这个值,但是我在C中遇到了委托的概念,我不确定我是否完全掌握了这个概念

是否可以将ProfileField委托给UserProfile类上的属性?还是我应该忘记它,去反思

你有什么建议

public partial class UserProfileForm : CustomIntranetWebappUserControl
{
    protected override void OnInit(EventArgs e)
    {
        //AutoEventWireup is set to false
        Load += Page_Load;
        CancelLinkButton.Click += CancelButtonClickEvent;
        SaveLinkButton.Click += SaveButtonClickEvent;
        base.OnInit(e);
    }

    private void SaveButtonClickEvent(object sender, EventArgs e)
    {
        VisitFormFields();
    }

    private void VisitFormFields()
    {
        var userProfileVisitor = new UserProfileVisitor();

        foreach (var control in Controls)
        {
            if (control is FormFieldUserControl)
            {
                var formField = (FormFieldUserControl) control;
                formField.Visit(userProfileVisitor);
            }
        }
        userProfileVisitor.Save();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindText();
        }
    }

    private void BindText()
    {
        LastNameFormLine.LabelText = string.Format("{0}:", HomePage.Localize("Last Name"));
        LastNameFormLine.InputValue = UserProfile.LastName;
        LastNameFormLine.IsMandatoryField = true;
        LastNameFormLine.IsMultilineField = false;
        LastNameFormLine.ProfileField = "UserProfile.LastName";
        //... the rest of this method is exactly like the 4 lines above.
    }
}

public abstract class FormFieldUserControl : CustomIntranetWebappUserControl
{
    public string ProfileField { get; set; }
    public abstract void Visit(UserProfileVisitor userProfileVisitor);
}


public partial class FormLineTextBox : FormFieldUserControl
{
//...  irrelevant code removed... 

    public override void Visit(UserProfileVisitor userProfileVisitor)
    {
        if (userProfileVisitor == null)
        {
            Log.Error("UserProfileVisitor not defined for the field: " + ProfileField);
            return;
        }
        userProfileVisitor.Visit(this);
    }
}

public class UserProfileVisitor
{

    public void Visit(FormLineTextBox formLine)
    {
        // The value of formLine.ProfileField is null!!!
        Log.Debug(string.Format("Saving form field type {1} with profile field [{0}] and value {2}", formLine.ProfileField, formLine.GetType().Name, formLine.InputValue));
    }

    // ... removing irrelevant code... 

    public void Save()
    {
        Log.Debug("Triggering the save operation...");
    }
}

委托不适用于属性。但是,反射速度很慢,可能存在代码安全性问题,而且它不是类型安全的,并且由于后期绑定的性质,可能导致命名错误的运行时问题而不是编译时问题


也就是说,您可能希望使用getter和/或setter方法,并在这些方法上使用委托。

请不要缩短链接。大多数人都希望在单击之前看到链接的位置。为什么要使控件可序列化?这没有道理。@埃雷伦德:刚刚编辑了这篇文章。谢谢你的邀请input@erikkallen:刚刚删除。如果不想让Getter/settersReflection比委派更快,可以使用属性和匿名方法。请阅读:@Xxxo感谢您的评论和否决票。在.NET世界中,a是函数指针概念的托管变体。提到的帖子是错误的;例如,已编译表达式的大小写为。我的回答仍然是正确的,您所指的博客文章没有使用正确的代表案例术语,对不起-另请参见博客文章的评论8、9和10。