这里有一个微观问题,关于为什么我想出的最终答案中需要向上转换(在本文的底部);以及一个关于我是否错过了“房间里的大象”的宏观问题:一些非常明显的简洁方式来做我想做的事[请不要问我-为什么-我想要我真正想要的;只要把它当作我想要这个的给定,它就是......]
我想通过 MongoDB.Bson CLR 程序集从 F# 初始化 BsonDocument。我认为我应该使用的 BsonDocument 构造函数的特定重载是
MongoDB.Bson.BsonDocument.BsonDocument(IDictionary<string,object>)
Run Code Online (Sandbox Code Playgroud)
这就是为什么我认为这是我应该使用的(以下是在类型花园中的漫长漫步......)
MongoDB 站点MongoDB CSharp 驱动程序教程中的 C# 示例使用集合初始值设定项语法,该语法映射到 BsonDocument 公开的接口上的 .Add 的一个或多个调用。教程示例类似于以下内容:
var bdoc = new BsonDocument { { "a", "1" }, { "b", "2" }, };
Run Code Online (Sandbox Code Playgroud)
我不确定正在使用 .Add 的哪个重载(并且不知道如何在 Visual Studio 中检查),但所有基于字典的重载都键入为 <string, object>。在这种情况下,每对中的第二个值,即字符串类型的“1”和“2”,自动(通过继承)也是对象类型,因此一切正常。.Add 的其他重载要求第二项的类型为 BsonValue,它是 BsonString 的抽象超类型,无论使用哪种重载,它都具有从 .NET 字符串的隐式转换;所以那里也一切都好。调用构造函数的哪个重载并不重要。
这有点难以转化为 F# 等效项,因为很难获取 BsonDocument 的 .Add 方法。我想到了
[("a", "1");("b", "2");] |> Seq.iter BsonDocument.Add
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为 BsonDocument.Add 不是静态方法;我可以实例化 BsonDocument,然后编写一个调用 BsonDocument 的 .Add 方法的 fun lambda,这至少会隔离 fun 的可变性:
[("a", "1");("b", "2");] …Run Code Online (Sandbox Code Playgroud) 我得到这个例外:
LogLoaderUnmanaged.exe 中 0x75374B32 (KernelBase.dll) 处的未处理异常:0xE0434352(参数:0x80070002、0x00000000、0x00000000、0x0000000004、0x000000004)。
当我使用此代码(Application .exe 类型项目的一部分)调用我的 CLR 项目时:
int _tmain(int argc, _TCHAR* argv[])
{
_tprintf_s(_T("Press enter to start logging messages."));
_getch();
std::string app("Application");
std::string domain("Domain");
std::string message("Message");
UnmanagedLoggerClient::LogError(Debug, app.c_str(), domain.c_str(), message.c_str());
_tprintf_s(_T("Done."));
}
Run Code Online (Sandbox Code Playgroud)
错误发生在对 LogError 的调用上,它在我的 CLR DLL 的头文件中定义如下:
#ifdef UNMANAGEDLOGGERCLIENT_EXPORTS
#define WIN32PROJECT_API __declspec(dllexport)
#else
#define WIN32PROJECT_API __declspec(dllimport)
#endif
enum UnmanagedLogLevel
{
Debug = 0,
Error = 1
};
static class WIN32PROJECT_API UnmanagedLoggerClient
{
public:
static void LogError (UnmanagedLogLevel level, const char* app, const char* domain, const …Run Code Online (Sandbox Code Playgroud) 这是我的班级。我在使用“literal”修饰符声明成员类型“name”时遇到错误。
ref class CreditCardAccount
{
public:
static CreditCardAccount ();
CreditCardAccount (long number, double limit);
void SetCreditCardLimit (double amount);
bool MakePurchase (double amount);
void MakeRepayment (double amount);
void PrintStatement ();
long GetAccountNumber ();
static short GetNumOfAccounts ();
literal String name = "Super Platinum Card";
private:
initonly long accountNumber;
double currentBalance;
double creditLimit;
static short numOfAccounts;
static double interestRate;
};
Run Code Online (Sandbox Code Playgroud)
当我尝试引用类型“名称”时出现错误,例如:
Console::Write("Card name is ");
Console::WriteLine(CreditCardAccount::name);
Run Code Online (Sandbox Code Playgroud)
错误:
error C2146: syntax error : missing ';' before identifier 'String'
error C4430: missing type specifier …Run Code Online (Sandbox Code Playgroud) 我正在创建一个C ++ / CLI DLL,该DLL应该用作包装器。它的目的是包装C#SDK并将函数呈现给本机C ++代码。我总是收到混合类型的错误,并且托管类中的using语句被标记为红色,因此,到目前为止,这是我得到的:
#pragma once
#include <iostream>
#include <memory>
#include <string>
namespace TPInterface
{
class ITPFactory
{
public:
static __declspec(dllexport) std::shared_ptr<ITPFactory> CreateTPFactory();
};
}
Run Code Online (Sandbox Code Playgroud)
这将创建TPFactory的实例。
#pragma once
#include "ITPSSITotalStation.h"
#include "TPSSITotalStation.h"
#include "ITPFactory.h"
#include <iostream>
#include <memory>
#include <string>
namespace TPInterface
{
class TPFactory : public ITPFactory
{
public:
static std::shared_ptr<SensorSoftwareInterface::TotalStation::ITPSSITotalStation> CreateTPSSITotalStation(std::string pathToDriver);
};
}
Run Code Online (Sandbox Code Playgroud)
这将创建一个TPSSITotalStation对象,该对象是ITPSSITotalStation接口。
TPSSITotalStation-> TPSSIBase->TPBase
二者TPSSIBase并TPBase含有被写入在托管代码(参照类)的类(gcroot和报头)。
现在,编译器告诉我,那些ref类是混合的,不允许的,依此类推。我在这里没问题...我在做什么错?
抱歉,我很笨,我是C ++的新手,来自C#。
错误:
Error 7 error C4368: cannot define 'm_selectedPath' …Run Code Online (Sandbox Code Playgroud) 根据 MSDN,
Managed Code - Code that could be understood and managed by CLR
Unmanaged Code - Code that could not handled by CLR
Run Code Online (Sandbox Code Playgroud)
但我想知道如何确定哪些资源或类是 CLR 兼容的,哪些不是。
我参考了下面的链接
我们如何区分 C# 中的托管和非托管资源?TextFieldParser 是否不受管理?
public class Customer
{
//What makes this CLR compliant
}
public class DbConnectionHandler: IDisposable
{
//Database Connection & other related stuff
//What makes this CLR Non-Compliant
}
Run Code Online (Sandbox Code Playgroud)如果我使用 EF (LINQ to Entities),它会被转换为原生 SQL。所以所有 LINQ 都属于非托管代码类别?
谢谢!
我只是想知道在没有SynchronizationContext(例如控制台)的应用程序中,是如何async static Main(args)实现的。是在任何awaita之前的 main 方法的开始
线程池线程,其中 CLR 本身有一个专用线程,该线程将Main调用排队到线程池并同步等待它完成?
或者
它是一个专用的启动线程,它被编译成一个特殊的状态机,在每次等待时同步阻塞线程。或者甚至可能所有await的都合并为一个,main线程等待这个合并的任务完成?
从性能的角度来看,自动实现的属性和手动实现的属性之间是否有任何区别?
我正在读关于Marshaling的事.我很困惑,因为在非托管代码中这意味着什么.HRESULT,DWORD和HANDLE.原文是:
您已经知道托管和非托管环境之间没有这种兼容性.换句话说,.NET不包含存在于非托管代码领域中的类型HRESULT,DWORD和HANDLE.因此,您需要找到.NET替代品或根据需要创建自己的替代品.这就是所谓的编组.
我找到了这个小脚本.我的问题是,如何在每一行之后清除cmd(cls)?
因此,当第一行完成时,它会清除cmd并显示下一行.
有人可以帮忙吗?:)
@echo off
:: Ghost typer
setlocal enableextensions enabledelayedexpansion
set lines=6
set "line1=Twinkle twinkle little star"
set "line2=How I wonder what you are"
set "line3=Up above the world so high"
set "line4=Like a diamond in the sky"
set "line5=Twinkle twinkle little star"
set "line6=How I wonder what you are"
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
for /L %%a in (1,1,%lines%) do set num=0&set "line=!line%%a!"&call :type
pause>nul
goto :EOF
:type
set …Run Code Online (Sandbox Code Playgroud) 我在C#中有一个扩展方法:
Foo(this Bar bar, int x)
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
我如何在F#中调用它?
bar.Foo(100); // Doesn't work
bar.Namespace.Foo(100); // Doesn't work
Run Code Online (Sandbox Code Playgroud)
如何在F#中的.NET列表中使用扩展方法Sum的答案?需要打开模块.我可以不使用扩展方法open吗?
如果我向到处使用的函数添加新的可选参数,大型项目中的现有代码是否有可能崩溃?我知道我可以重载该函数并将风险降到最低,但实际上......如果我坚持使用可选参数,会有什么风险?
这是一个例子:
Public Function GetContent(ByVal URL As String, ByVal ID As String, Optional ByRef PageTitle As String = "") As String
Try
Dim web As New HtmlWeb()
Dim doc As HtmlDocument = web.Load(URL)
ID = "//div[@id='" & ID & "']"
Dim ContentNode As HtmlNode = doc.DocumentNode.SelectSingleNode(ID)
' The two lines below are the mere extent of what's new inside this function, besides the new Optional ByRef parameter in its signature
Dim PageTitleNode As HtmlNode = doc.DocumentNode.SelectSingleNode("//title")
If Not PageTitleNode Is …Run Code Online (Sandbox Code Playgroud) 在C#Int类型中派生自ValueType.ValueType是从Object类型派生的.因此Int是一个Object.
如果每个值类型变量已经是一个Object,那么在装箱/拆箱期间实际发生了什么?
众所周知,在C#中迭代一些IEnumerable时,不能对可枚举集合的元素进行修改:
// Illegal code
foreach (Employee e in employeeList)
{
e.Salary = 1000000;
}
Run Code Online (Sandbox Code Playgroud)
我想知道运行时或枚举器本身是如何强制执行的?
clr ×13
c# ×8
.net ×2
c++ ×2
c++-cli ×2
f# ×2
unmanaged ×2
asp.net-core ×1
async-await ×1
asynchronous ×1
boxing ×1
bson ×1
clr4.0 ×1
cmd ×1
com ×1
exception ×1
line ×1
managed ×1
marshalling ×1
mongodb ×1
overloading ×1
signature ×1
text ×1
typing ×1
unboxing ×1