1

IsClientScriptIncludeRegistered前にチェックすることの利点はどれほど重要RegisterClientScriptIncludeですか?

ドキュメントの例から:

' Check to see if the include script is already registered.
If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then    
    cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
End If

どの州:

既存のクライアントスクリプトインクルードをチェックするロジックが削除された場合でも、RegisterClientScriptIncludeメソッドが重複をチェックするため、レンダリングされたページに重複するクライアントスクリプトは存在しないことに注意してください。チェックの利点は、不要な計算を減らすことです。

強調表示された文について疑問に思っています。どんな不必要な計算?

私にはそれは逆のようです。IsClientScriptIncludeRegisteredbeforeを使用RegisterClientScriptIncludeすると、登録されたスクリプトを2回チェックする必要があります(したがって、削減される代わりに不要な計算が追加されます)。

ここで何が欠けていますか?

4

1 に答える 1

1

スクリプトがすべて準備完了している場合、辞書でのIsClientScriptIncludeRegistered簡単なチェックは、多くのコードがなく、非常に高速なチェックです。

public bool IsClientScriptIncludeRegistered(Type type, string key)
{
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    return ((this._registeredClientScriptBlocks != null) && 
     this._registeredClientScriptBlocks.Contains(
        CreateScriptIncludeKey(type, key, false)));
}

一方RegisterClientScriptInclude、からは、既存のチェックに到達するまで、もう少しコードが含まれています。

internal void RegisterClientScriptInclude(Control control, Type type, string key, string url)
{
    IScriptManager scriptManager = this._owner.ScriptManager;
    if ((scriptManager != null) && scriptManager.SupportsPartialRendering)
    {
        scriptManager.RegisterClientScriptInclude(control, type, key, url);
    }
    else
    {
        this.RegisterClientScriptInclude(type, key, url);
    }
}

internal void RegisterClientScriptInclude(Type type, string key, string url, bool isResource)
{
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    if (string.IsNullOrEmpty(url))
    {
        throw ExceptionUtil.ParameterNullOrEmpty("url");
    }
    string script = "\r\n<script src=\"" + HttpUtility.HtmlAttributeEncode(url) + "\" type=\"text/javascript\"></script>";
    this.RegisterScriptBlock(CreateScriptIncludeKey(type, key, isResource), script, ClientAPIRegisterType.ClientScriptBlocks);
}

internal void RegisterScriptBlock(ScriptKey key, string script, ClientAPIRegisterType type)
{
    switch (type)
    {
        case ClientAPIRegisterType.ClientScriptBlocks:
            this.RegisterScriptBlock(key, script, ref this._registeredClientScriptBlocks, ref this._clientScriptBlocks, false);
            break;

        case ClientAPIRegisterType.ClientScriptBlocksWithoutTags:
            this.RegisterScriptBlock(key, script, ref this._registeredClientScriptBlocks, ref this._clientScriptBlocks, true);
            break;

        case ClientAPIRegisterType.ClientStartupScripts:
            this.RegisterScriptBlock(key, script, ref this._registeredClientStartupScripts, ref this._clientStartupScripts, false);
            break;

        case ClientAPIRegisterType.ClientStartupScriptsWithoutTags:
            this.RegisterScriptBlock(key, script, ref this._registeredClientStartupScripts, ref this._clientStartupScripts, true);
            break;
    }
    if (this._owner.PartialCachingControlStack != null)
    {
        foreach (BasePartialCachingControl control in this._owner.PartialCachingControlStack)
        {
            control.RegisterScriptBlock(type, key, script);
        }
    }
}


private void RegisterScriptBlock(ScriptKey key, string script, ref ListDictionary scriptBlocks, ref ArrayList scriptList, bool needsScriptTags)
{
    if (scriptBlocks == null)
    {
        scriptBlocks = new ListDictionary();
        scriptList = new ArrayList();
    }
    if (!scriptBlocks.Contains(key))
    {
        Tuple<ScriptKey, string, bool> tuple = new Tuple<ScriptKey, string, bool>(key, script, needsScriptTags);
        scriptBlocks.Add(key, null);
        scriptList.Add(tuple);
    }
}
于 2013-03-03T03:05:38.927 に答える