Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
tsai
Dapper
Commits
bebe80bc
Commit
bebe80bc
authored
Feb 26, 2016
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support get-only C# 6 properties, i.e. `public int Id {get;}`
parent
b9288b4a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
6 deletions
+32
-6
Dapper.Tests.xproj
Dapper.Tests/Dapper.Tests.xproj
+3
-1
Tests.cs
Dapper.Tests/Tests.cs
+13
-1
DefaultTypeMap.cs
Dapper/DefaultTypeMap.cs
+15
-3
SqlMapper.cs
Dapper/SqlMapper.cs
+1
-1
No files found.
Dapper.Tests/Dapper.Tests.xproj
View file @
bebe80bc
...
@@ -14,7 +14,9 @@
...
@@ -14,7 +14,9 @@
<PropertyGroup>
<PropertyGroup>
<SchemaVersion>
2.0
</SchemaVersion>
<SchemaVersion>
2.0
</SchemaVersion>
</PropertyGroup>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"
/>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"
>
<ProduceOutputsOnBuild>
True
</ProduceOutputsOnBuild>
</PropertyGroup>
<ItemGroup>
<ItemGroup>
<Service
Include=
"{82a7f48d-3b50-4b1e-b82e-3ada8210c358}"
/>
<Service
Include=
"{82a7f48d-3b50-4b1e-b82e-3ada8210c358}"
/>
</ItemGroup>
</ItemGroup>
...
...
Dapper.Tests/Tests.cs
View file @
bebe80bc
...
@@ -3696,5 +3696,17 @@ protected static SqliteConnection GetSqliteConnection(bool open = true)
...
@@ -3696,5 +3696,17 @@ protected static SqliteConnection GetSqliteConnection(bool open = true)
return
connection
;
return
connection
;
}
}
#endif
#endif
}
[
Fact
]
public
void
GetOnlyProperties
()
{
var
obj
=
connection
.
QuerySingle
<
HazGetOnly
>(
"select 42 as [Id], 'def' as [Name];"
);
obj
.
Id
.
IsEqualTo
(
42
);
obj
.
Name
.
IsEqualTo
(
"def"
);
}
class
HazGetOnly
{
public
int
Id
{
get
;
}
public
string
Name
{
get
;
}
=
"abc"
;
}
}
}
}
Dapper/DefaultTypeMap.cs
View file @
bebe80bc
...
@@ -164,13 +164,25 @@ public SqlMapper.IMemberMap GetMember(string columnName)
...
@@ -164,13 +164,25 @@ public SqlMapper.IMemberMap GetMember(string columnName)
if
(
property
!=
null
)
if
(
property
!=
null
)
return
new
SimpleMemberMap
(
columnName
,
property
);
return
new
SimpleMemberMap
(
columnName
,
property
);
// roslyn automatically implemented properties, in particular for get-only properties: <{Name}>k__BackingField;
var
backingFieldName
=
$"<
{
columnName
}
>k__BackingField"
;
// preference order is:
// exact match over underscre match, backing fields over regular fields, exact case over wrong case
var
field
=
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
columnName
,
StringComparison
.
Ordinal
))
var
field
=
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
columnName
,
StringComparison
.
Ordinal
))
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
columnName
,
StringComparison
.
OrdinalIgnoreCase
));
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
columnName
,
StringComparison
.
OrdinalIgnoreCase
))
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
backingFieldName
,
StringComparison
.
Ordinal
))
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
backingFieldName
,
StringComparison
.
OrdinalIgnoreCase
));
if
(
field
==
null
&&
MatchNamesWithUnderscores
)
if
(
field
==
null
&&
MatchNamesWithUnderscores
)
{
{
field
=
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
columnName
.
Replace
(
"_"
,
""
),
StringComparison
.
Ordinal
))
var
effectiveColumnName
=
columnName
.
Replace
(
"_"
,
""
);
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
columnName
.
Replace
(
"_"
,
""
),
StringComparison
.
OrdinalIgnoreCase
));
backingFieldName
=
$"<
{
effectiveColumnName
}
>k__BackingField"
;
field
=
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
effectiveColumnName
,
StringComparison
.
Ordinal
))
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
effectiveColumnName
,
StringComparison
.
OrdinalIgnoreCase
))
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
backingFieldName
,
StringComparison
.
Ordinal
))
??
_fields
.
FirstOrDefault
(
p
=>
string
.
Equals
(
p
.
Name
,
backingFieldName
,
StringComparison
.
OrdinalIgnoreCase
));
}
}
if
(
field
!=
null
)
if
(
field
!=
null
)
...
...
Dapper/SqlMapper.cs
View file @
bebe80bc
...
@@ -2798,7 +2798,7 @@ static LocalBuilder GetTempLocal(ILGenerator il, ref Dictionary<Type, LocalBuild
...
@@ -2798,7 +2798,7 @@ static LocalBuilder GetTempLocal(ILGenerator il, ref Dictionary<Type, LocalBuild
)
)
{
{
var
returnType
=
type
.
IsValueType
()
?
typeof
(
object
)
:
type
;
var
returnType
=
type
.
IsValueType
()
?
typeof
(
object
)
:
type
;
var
dm
=
new
DynamicMethod
(
$"Deserialize
{
Guid
.
NewGuid
()}
"
,
returnType
,
new
[]
{
typeof
(
IDataReader
)
},
true
);
var
dm
=
new
DynamicMethod
(
$"Deserialize
{
Guid
.
NewGuid
()}
"
,
returnType
,
new
[]
{
typeof
(
IDataReader
)
},
t
ype
,
t
rue
);
var
il
=
dm
.
GetILGenerator
();
var
il
=
dm
.
GetILGenerator
();
il
.
DeclareLocal
(
typeof
(
int
));
il
.
DeclareLocal
(
typeof
(
int
));
il
.
DeclareLocal
(
type
);
il
.
DeclareLocal
(
type
);
...
...
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