Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Thick-Common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
潘栩锋
Thick-Common
Commits
d04a2661
Commit
d04a2661
authored
Mar 20, 2020
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
本来想添加多级绑定功能,像Binding 一样。 但GetValue( obj, new object[]{index}); 失败。 放弃!
parent
d91e9ad3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
0 deletions
+39
-0
PropertiesManager.cs
Project.FLY.Misc/MISC/PropertiesManager.cs
+39
-0
No files found.
Project.FLY.Misc/MISC/PropertiesManager.cs
View file @
d04a2661
...
...
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using
System.Linq
;
using
System.Reflection
;
using
System.Text
;
using
System.Text.RegularExpressions
;
namespace
Misc
{
...
...
@@ -28,6 +29,8 @@ namespace Misc
/// <param name="v"></param>
public
static
void
SetValue
(
object
obj
,
string
propertyName
,
object
v
)
{
//if (!getRootProperty(ref obj, ref propertyName))
// return;
PropertyInfo
property
=
obj
.
GetType
().
GetProperty
(
propertyName
);
if
(
property
!=
null
)
{
...
...
@@ -35,6 +38,36 @@ namespace Misc
}
}
static
bool
getRootProperty
(
ref
object
obj
,
ref
string
propertyName
)
{
Regex
r
=
new
Regex
(
@"(\S+)\[(\d+)\]"
);
string
[]
lvs
=
propertyName
.
Split
(
'.'
);
if
(
lvs
.
Count
()
>
1
)
{
//这个是多级的
for
(
int
i
=
0
;
i
<
lvs
.
Count
()
-
1
;
i
++)
{
PropertyInfo
property
;
string
name
=
lvs
[
i
];
int
index
=
0
;
var
match
=
r
.
Match
(
name
);
if
(
match
.
Success
)
{
name
=
match
.
Groups
[
1
].
Value
;
index
=
int
.
Parse
(
match
.
Groups
[
2
].
Value
);
}
property
=
obj
.
GetType
().
GetProperty
(
name
);
if
(
property
==
null
)
return
false
;
//异常
if
(
match
.
Success
)
obj
=
property
.
GetValue
(
obj
,
new
object
[]
{
index
});
else
obj
=
property
.
GetValue
(
obj
);
}
propertyName
=
lvs
.
Last
();
}
return
true
;
}
/// <summary>
/// 设定参数(属性)的值,触发属性值变化“通知”,writeVal
/// </summary>
...
...
@@ -43,6 +76,9 @@ namespace Misc
/// <param name="val"></param>
public
static
void
SetValue
(
object
obj
,
string
propertyName
,
string
val
)
{
//if (!getRootProperty(ref obj, ref propertyName))
// return;
PropertyInfo
property
=
obj
.
GetType
().
GetProperty
(
propertyName
);
if
(
property
!=
null
)
{
...
...
@@ -59,6 +95,9 @@ namespace Misc
/// <returns></returns>
public
static
object
GetValue
(
object
obj
,
string
propertyName
)
{
//if (!getRootProperty(ref obj, ref propertyName))
// return null;
PropertyInfo
property
=
obj
.
GetType
().
GetProperty
(
propertyName
);
if
(
property
!=
null
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment