使用 C# 将对象属性分配给 SAP 中的字符串变量
Assign object attribute to a String variable in SAP using c#
假设我创建了一个名为 oItem
的对象。而且这个有很多属性比如ItemCode
,ItemName
.......(300左右)。我想为我的应用程序用户选择的其中一些属性分配新值。用户将这些属性名称作为字符串。
例如:string attribute1 = "ItemCode"
.
现在我要做的是为这个属性赋值,例如:
oItem.attribute1 = "01234";
有没有办法做这样的事情?我知道您可以将 c# 函数调用转换为字符串。因此我认为这也应该是可能的。任何帮助将不胜感激。谢谢!
更新:这是我的 SAP 插件的一部分。所以这些属性来自数据库 table。困难的部分是用户可以向其中添加更多列(用户定义的字段),这会增加属性的数量,而且我只知道原来的 300 个属性。
如果你有 300 个属性,你真的应该重构这个 class。我认为您可以在这种情况下使用 Dictionarystring, string>
。
Dictionary<string, string> Items = new Dictionary<string, string>
{
{"attribute1", "01234"}, {"attribute2", "56789"}, {"attribute3", "76543"}, // ...
};
您可以非常有效地访问这些值:
string attribute1 = Items["attribute1"];
或add/modify他们以类似的方式:
Items["attribute4"] = "23456";
为了方便您,C# 中有一个 "dynamic" 关键字。
来自 msdn dynamic object article 的示例:
// 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
使用带有提供的库函数的 c# 为 SAP B1 插件执行此操作真的很容易。只需包含 using SAPbobsCOM;
然后可以按如下方式进行
yourobject.UserFields.Fields.Item("attribute").Value = "value";
假设我创建了一个名为 oItem
的对象。而且这个有很多属性比如ItemCode
,ItemName
.......(300左右)。我想为我的应用程序用户选择的其中一些属性分配新值。用户将这些属性名称作为字符串。
例如:string attribute1 = "ItemCode"
.
现在我要做的是为这个属性赋值,例如:
oItem.attribute1 = "01234";
有没有办法做这样的事情?我知道您可以将 c# 函数调用转换为字符串。因此我认为这也应该是可能的。任何帮助将不胜感激。谢谢!
更新:这是我的 SAP 插件的一部分。所以这些属性来自数据库 table。困难的部分是用户可以向其中添加更多列(用户定义的字段),这会增加属性的数量,而且我只知道原来的 300 个属性。
如果你有 300 个属性,你真的应该重构这个 class。我认为您可以在这种情况下使用 Dictionarystring, string>
。
Dictionary<string, string> Items = new Dictionary<string, string>
{
{"attribute1", "01234"}, {"attribute2", "56789"}, {"attribute3", "76543"}, // ...
};
您可以非常有效地访问这些值:
string attribute1 = Items["attribute1"];
或add/modify他们以类似的方式:
Items["attribute4"] = "23456";
为了方便您,C# 中有一个 "dynamic" 关键字。 来自 msdn dynamic object article 的示例:
// 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
使用带有提供的库函数的 c# 为 SAP B1 插件执行此操作真的很容易。只需包含 using SAPbobsCOM;
然后可以按如下方式进行
yourobject.UserFields.Fields.Item("attribute").Value = "value";