gpt4 book ai didi

.net - jquery 类似 c#/.Net 中的扩展

转载 作者:行者123 更新时间:2023-12-02 15:09:48 25 4
gpt4 key购买 nike

Jquery 有一个名为“extend”的函数。 ' 可用于合并对象。将默认选项与用户指定的选项合并,以获得有效的选项是非常有用的。

c#/.Net中有类似的函数吗?

class settings
{
public string Directory;
public string Username;
}
settings default_set = new settings(){ Directory = "C:/" , Username = "admin" };
settings user_set = new settings(){ Username = "john" };

settings merged = new settings(){ Directory = "C:/" , Username = "john" };

有什么方法可以从另外两个对象中获取合并的数据吗?是否有任何通用函数或者我应该使用 Reflection 进行一些编码?

最佳答案

我创建了一个类似于RouteValueDictionary的通过合并功能实现此目的。

用法:

var defaultSet = new ParameterDictionary( new { Directory = "C:/", Username = "admin" } );
defaultSet["Username"] = "john"; // contains C:/ and "john"
defaultSet.Merge( new { Directory = "D:/" } ); // contains D:/ and "john"
defaultSet.Merge( new { Directory = "C:/", Username = "admin" } ); // back to original

实现:

public class ParameterDictionary : Dictionary<string, object>
{
public ParameterDictionary()
: base()
{
}

/// <summary>
/// Build the parameter dictionary from the public properties of the supplied object
/// where each key is a property name and each value is the corresponding property's
/// valuye.
/// </summary>
/// <param name="parameters">An object containg the properties making up the initial key/value dictionary state.</param>
public ParameterDictionary( object parameters )
: this()
{
if (parameters != null)
{
if (parameters is IDictionary)
{
this.Merge( parameters as IDictionary, true );
}
else
{
foreach (PropertyInfo info in parameters.GetType().GetProperties())
{
object value = info.GetValue( parameters, null );
this.Add( info.Name, value );
}
}
}
}

/// <summary>
/// Merge a dictionary of keys/values with the current dictionary.
/// </summary>
/// <param name="dict">The dictionary whos parameters will be added. Must have string keys.</param>
/// <param name="replace">True if conflicting parameters should replace the existing ones, false otherwise.</param>
/// <returns>The updated Parameter dictionary.</returns>
public ParameterDictionary Merge( IDictionary dict, bool replace )
{
foreach (string key in dict.Keys)
{
if (this.ContainsKey( key ))
{
if (replace)
{
this[key] = dict[key];
}
}
else
{
this.Add( key, dict[key] );
}
}
return this;
}

/// <summary>
/// Merge a dictionary of keys/values with the current dictionary. Replaces conflicting keys.
/// </summary>
/// <param name="dict">The dictionary whos parameters will be added. Must have string keys.</param>
/// <returns>The updated Parameter dictionary.</returns>
public ParameterDictionary Merge( object dict )
{
return Merge( dict, true );
}

/// <summary>
/// Merge a dictionary of keys/values with the current dictionary.
/// </summary>
/// <param name="dict">An object whose properties are used as the new keys/values for the update.</param>
/// <param name="replace">True if conflicting parameters should replace the existing ones, false otherwise.</param>
/// <returns>The updated Parameter dictionary.</returns>
public ParameterDictionary Merge( object dict, bool replace )
{
ParameterDictionary newDict = new ParameterDictionary( dict );
return Merge( newDict, replace );
}
}

关于.net - jquery 类似 c#/.Net 中的扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1306195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com