이 정규식은 입력 문자열과 일치해야합니다.
var regex = new Regex(
@"\(\s*((?<PropertyName>.*?)\s*((:=)|(=:))\s*(?<PropertyValue>.*?)\s*(&&)?\s*)*\)");
var match = regex.Match(yourString);
그런 다음 각 속성/값 쌍을 통해 루프,
설정을이와
\( Open paren
\s* Optional whitespace
(
(?<PropertyName>.*?) Property name group
\s* Optional whitespace
((:=)|(=:)) Literal ':=' or '=:'
\s* Optional whitespace
(?<PropertyValue>.*?) Property value group
\s* Optional whitespace
(&&)? Optional '&&' (the last one won't have one)
\s* Optional whitespace
)* The proceeding can repeat 0-many times
\) Close paren
, 당신은 C#으로 당신의 문자열에 대해 일치를 수행 할 수 있습니다 여기에
\(\s*((?<PropertyName>.*?)\s*((:=)|(=:))\s*(?<PropertyValue>.*?)\s*(&&)?\s*)*\)
는 의미입니다 개체의 속성 객체 속성을 설정하려면 객체 속성 유형에 따라 약간의 반사와 다른 코드가 필요합니다.
var properyNames = match.Groups["PropertyName"].Captures;
var properyValues = match.Groups["PropertyValue"].Captures;
var numPairs = propertyNames.Count;
var objType = yourObj.GetType();
for (var i = 0; i < numPairs; i++)
{
var propertyName = propertyNames[i].Value;
var theValue = propertyValues[i].Value;
var property = objType.GetProperty(propertyName);
object convertedValue = theValue;
if (property.PropertyType == typeof(int))
convertedValue = int.Parse(theValue);
if (property.PropertyType == typeof(DateTime))
// ....
// etc....
property.SetValue(yourObj, convertedValue, null);
}
다음과 같은 오타가 있습니까? –
아니요, 누군가가 불일치 공백 및 같음을 사용하여 DB를 수동으로 편집했습니다. – Lincoln