使用c#将对象属性分配给SAP中的String变量
本文关键字:SAP 中的 String 变量 分配 对象 属性 使用 | 更新日期: 2023-09-27 18:24:00
假设我创建了一个对象名oItem
。这有很多属性,比如ItemCode
、ItemName
。。。。。(约300)。我想为这些属性中的一些属性分配新的值,这些属性是由我的应用程序的用户选择的。用户将以字符串形式提供这些属性名称。
例如:string attribute1 = "ItemCode"
。
现在我想做的是给这个属性赋值,比如:
oItem.attribute1 = "01234";
有办法做这样的事情吗?我知道你可以将c函数调用转换为字符串。因此,我认为这也是可能的。如有任何帮助,我们将不胜感激。谢谢
更新:这是我的SAP附加组件的一部分。所以这些属性来自数据库表。困难的是,用户可以添加更多的列(用户定义的字段),这增加了属性的数量,而且我只知道最初的300个属性。
如果你有300个属性,你真的应该重构这个类。我认为在这种情况下你可以使用Dictionarystring, string>
。
Dictionary<string, string> Items = new Dictionary<string, string>
{
{"attribute1", "01234"}, {"attribute2", "56789"}, {"attribute3", "76543"}, // ...
};
您可以非常有效地访问值:
string attribute1 = Items["attribute1"];
或者以类似的方式添加/修改它们:
Items["attribute4"] = "23456";
为了让您更轻松,C#中有一个"dynamic"关键字。msdn动态对象文章示例:
// The class derived from DynamicObject.
public class DynamicDictionary : DynamicObject
{
// The inner dictionary.
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// This property returns the number of elements
// in the inner dictionary.
public int Count
{
get
{
return dictionary.Count;
}
}
// If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower();
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dictionary.TryGetValue(name, out result);
}
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] = value;
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
}
class Program
{
static void Main(string[] args)
{
// Creating a dynamic dictionary.
dynamic person = new DynamicDictionary();
// Adding new dynamic properties.
// The TrySetMember method is called.
person.FirstName = "Ellen";
person.LastName = "Adams";
// Getting values of the dynamic properties.
// The TryGetMember method is called.
// Note that property names are case-insensitive.
Console.WriteLine(person.firstname + " " + person.lastname);
// Getting the value of the Count property.
// The TryGetMember is not called,
// because the property is defined in the class.
Console.WriteLine(
"Number of dynamic properties:" + person.Count);
// The following statement throws an exception at run time.
// There is no "address" property,
// so the TryGetMember method returns false and this causes a
// RuntimeBinderException.
// Console.WriteLine(person.address);
}
}
// This example has the following output:
// Ellen Adams
// Number of dynamic properties: 2
对于SAP B1插件来说,使用c#和所提供的库函数确实很容易做到这一点。只需包含using SAPbobsCOM;
然后你可以按照
yourobject.UserFields.Fields.Item("attribute").Value = "value";