我有以下对象:
dynamic person = new {Id = 1, Name = "SpiderMan"};
Run Code Online (Sandbox Code Playgroud)
我需要能够遍历属性名称,例如"Id","Name".
我还需要能够以最有效的方式实现这一点,因此我选择使用FastMember,但它的api不允许我遍历属性.
有任何想法吗?
[UPDATE]
感谢Marc,我成功实现了我想要的用途:
dynamic person = new { Id = 1, Name = "SpiderMan" };
MemberSet members = TypeAccessor.Create(person.GetType()).GetMembers();
foreach (Member item in members)
{
// do whatever
}
Run Code Online (Sandbox Code Playgroud) 在我的Global.asax.cs的Application_Start中,我试图使用以下方法获取当前的应用程序路径:
var virtualPath = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
+ HttpRuntime.AppDomainAppVirtualPath;
Run Code Online (Sandbox Code Playgroud)
这将返回例如: http://localhost:99/MySite/
然后我将使用此URL并执行以下操作:
var pageToHit = virtualPath + Pages\MyOtherPage.aspx
var client = new WebClient();
client.DownloadData(dummyPageUrl);
Run Code Online (Sandbox Code Playgroud)
当我在IIS 6或Visual Studio内置Web服务器中运行项目时,所有这一切都很好,但是当我得到"System.Web.HttpException:请求在此上下文中不可用"时,IIS 7中的事情变得疯狂.
我知道这个帖子:请求在此上下文中不可用
但是,我想知道是否有人知道如何在不改变项目的情况下执行上述操作以在经典模式下运行.
我有一个非常简单的Web Api v2.2自我托管在OWIN
public class StartUp
{
public void Configuration(IAppBuilder appBuilder, IConfigReader configReader)
{
var container = new Container();
container.Register<IConfigReader, ConfigReader>();
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
appBuilder.UseWebApi(config);
}
}
Run Code Online (Sandbox Code Playgroud)
当我然后使用我Main()的:
WebApp.Start(baseAddress, appBuilder =>
new StartUp().Configuration(appBuilder, new ConfigReader()));
Run Code Online (Sandbox Code Playgroud)
但是当我尝试执行最后一行时,appBuilder.UseWebApi(config);我得到以下异常:
SimpleInjector.dll中发生了'SimpleInjector.ActivationException'类型的第一次机会异常
附加信息:给定类型IHostBufferPolicySelector不是具体类型.请使用其他一个重载来注册此类型.
完整堆栈:
发生SimpleInjector.ActivationException _HResult = -2146233088
_message =给定类型IHostBufferPolicySelector不是具体类型.请使用其他一个重载来注册此类型.
HResult = -2146233088 IsTransient = false Message =给定类型IHostBufferPolicySelector不是具体类型.请使用其他一个重载来注册此类型.Source = SimpleInjector
StackTrace:at SimpleInjector.Advanced.DefaultConstructorResolutionBehavior.VerifyTypeIsConcrete(Type …
self-hosting simple-injector asp.net-web-api owin asp.net-web-api2
我有以下查询:
SELECT
[Person].[Id] AS [Person_Id],
[Person].[Name] AS [Person_Name],
[Address].[Id] AS [Address_Id],
[Address].[Number] AS [Address_Number],
[Address].[Street] AS [Address_Street],
FROM [Person]
LEFT JOIN [Address] ON [Person].[addressId] = [Address].[Id]
Run Code Online (Sandbox Code Playgroud)
其中用于查询SQLite内存数据库:
var rows = _database.Query(query);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试读取值时,Int32我得到了InvalidCastException.
foreach (IDictionary<string, object> row in rows)
{
var someId = (int)row["Person_Id"];
var someNumber = (int)row["Address_Number"];
}
Run Code Online (Sandbox Code Playgroud)
我这样使用的原因dapper是,作为一个辅助项目,我正在构建一些功能,dapper以将值映射到POCO(我知道类似的项目,例如rainbow,dapper.extensions等).
所以有些东西:
var rowVal = row[fieldName];
propInfo.SetValue(obj, rowVal, null);
Run Code Online (Sandbox Code Playgroud)
同样,忽略与反射调用相关的性能成本.
在上面的例子中,属性类型POCO是int32因为这个问题我无法将值赋给obj …
给出一张表:
CREATE TABLE Foo(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT
);
Run Code Online (Sandbox Code Playgroud)
如何使用以下命令返回同时插入的多行的ID:
INSERT INTO Foo (Name) VALUES
('A'),
('B'),
('C');
Run Code Online (Sandbox Code Playgroud)
我知道last_insert_rowid()但我没有找到任何将它用于多行的示例.
在这个SQL Server示例中可以看到我想要实现的目标:
DECLARE @InsertedRows AS TABLE (Id BIGINT);
INSERT INTO [Foo] (Name) OUTPUT Inserted.Id INTO @InsertedRows VALUES
('A'),
('B'),
('C');
SELECT Id FROM @InsertedRows;
Run Code Online (Sandbox Code Playgroud)
很感谢任何形式的帮助.
我有一个 SQL Server 表,其中使用以下命令插入行:
var sql = @"
DECLARE @InsertedRows AS TABLE (Id BIGINT);
INSERT INTO Person ([Name], [Age]) OUTPUT Inserted.Id INTO @InsertedRows
VALUES (@Name, @Age);
SELECT Id FROM @InsertedRows;";
Person person = ...;
var id = connection.Query<long>(sql, person).First();
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但是如果我尝试插入多个项目并使用以下命令返回所有插入的 id:
IEnumerable<Person> people = ...;
var ids = connection.Query<long>(sql, people);
Run Code Online (Sandbox Code Playgroud)
我收到错误:
System.InvalidOperationException :在 Dapper.SqlMapper.GetCacheInfo(Identity Identity, Object exampleParameters, Boolean addToCache)
at Dapper.SqlMapper.d__23`1.MoveNext() 的上下文中不允许使用可枚举的参数序列(数组、列表等)
--- 从先前抛出异常的位置开始的堆栈跟踪结束 ---
如何在 Dapper 中返回多个插入的 id?
鉴于以下内容:
var dic = new Dictionary<string, (int, int)>()
{
["A"] = (1, 2)
};
dic.TryGetValue("A", out (int, int) value);
Run Code Online (Sandbox Code Playgroud)
我可以轻松地从value字典中删除,但是我如何解构它以获得每个单独的值,如下所示:
dic.TryGetValue("A", out var (left, right));
Run Code Online (Sandbox Code Playgroud) 我有一个条形图,显示Y轴上的不同类别.
我可以通过使用以下方法同时更改轴上所有这些颜色:
chart.ChartAreas["MyChart"].AxisY.LabelStyle.ForeColor = "Red";
Run Code Online (Sandbox Code Playgroud)
但是它不允许我为每个设置颜色.
任何帮助都感激不尽.
我正在使用以下代码向我的SQLite db文件编写自定义PRAGMA:
using (var db = GetNewConnection())
{
var version = "1234";
var query = string.Format("PRAGMA user_version={0}", version);
db.ExecuteSql(query);
}
Run Code Online (Sandbox Code Playgroud)
哪个成功地将PRAGMA写入文件,我可以通过执行以下命令使用SQLite Expert或LINQPad来检查:
PRAGMA user_version
Run Code Online (Sandbox Code Playgroud)
但是如何使用OrmLite v3.9.71从DB文件中读取PRAGMA的值?
我已经尝试过以下但它无法解析SQL,因为它无法找到"FROM":
db.Select<object>("PRAGMA user_version");
Run Code Online (Sandbox Code Playgroud)
我也试过以下,没有一个工作:
db.Select<dynamic>("PRAGMA user_version");
db.Select<string>("PRAGMA user_version");
db.Select<int>("PRAGMA user_version");
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在处理一个需要从一系列可能很大的文本文件(~3 + GB)中随机读取整行文本的应用程序.
线条可以具有不同的长度.
为了减少GC和创建不必要的字符串,我使用的解决方案是:有更好的方法来确定大型txt文件(1-2 GB)中的行数吗?检测每个新行并将其存储在一个映射中,从而产生一个索引lineNo => position:
// maps each line to it's corresponding fileStream.position in the file
List<int> _lineNumberToFileStreamPositionMapping = new List<int>();
Run Code Online (Sandbox Code Playgroud)
new line增量lineCount并添加fileStream.Position到_lineNumberToFileStreamPositionMapping然后我们使用类似于以下的API:
public void ReadLine(int lineNumber)
{
var getStreamPosition = _lineNumberToFileStreamPositionMapping[lineNumber];
//... set the stream position, read the byte array, convert to string etc.
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案目前提供了良好的性能,但有两件事我不喜欢:
array因此我无法预先分配,因此我必须使用List<int>具有调整大小的潜在低效率的实际需要的两倍;任何想法都非常感谢.
c# ×7
.net ×4
dapper ×2
sqlite ×2
asp.net ×1
asp.net-mvc ×1
bar-chart ×1
c#-7.0 ×1
charts ×1
dynamic ×1
fastmember ×1
filestream ×1
iis ×1
iis-7 ×1
indexing ×1
mschart ×1
owin ×1
self-hosting ×1
sql-server ×1
value-type ×1