我有一个使用Enum Generic Type的Generic类.我的问题如何在该类型的实例上使用GetEnumName?
我已经创建了一个小型演示类来说明问题:
type
TEnumSettings<TKey: record > = class
private
Key: TKey;
public
constructor Create(aKey: TKey);
function ToString: string; override;
end;
uses
TypInfo;
{ TEnumSettings<TKey> }
constructor TEnumSettings<TKey>.Create(aKey: TKey);
begin
if PTypeInfo(System.TypeInfo(TKey)).Kind <> tkEnumeration then
Exception.Create(string(PTypeInfo(System.TypeInfo(TKey)).Name) + ' is not an Enumeration');
Key := aKey;
end;
function TEnumSettings<TKey>.ToString: string;
begin
Result := GetEnumName(System.TypeInfo(TKey), Integer(Key)) <== HERE I get a compile error: Invalid type cast
end;
Run Code Online (Sandbox Code Playgroud)
我正在使用Delphi XE.那可以这样做吗?如果是这样怎么样?
我正和TValue一起玩
我在一个空白项目中编写了这段代码:
uses
RTTI;
procedure TForm1.FormCreate(Sender: TObject);
var
s: string;
b: Boolean;
begin
s := TValue.From<Boolean > (True).ToString;
b := TValue.From<string > (s).AsType<Boolean>;
end;
Run Code Online (Sandbox Code Playgroud)
但我无法从字符串转换回布尔值; 我在第二行得到一个无效的Typecast异常.
我正在使用Delphi XE,但它与Delphi Xe6中的结果相同,这导致我得出结论:我使用的是TValue错误.
那么请问我做错了什么.
AttachConsole当程序编译为64位时,WinAPI函数始终返回true。
首先,我声明了以下函数:
function AttachConsole(dwProcessId: DWORD): Bool; stdcall; external KERNEL32 name 'AttachConsole';
Run Code Online (Sandbox Code Playgroud)
然后我调用我的函数:
if AttachConsole(DWORD(-1)) then
....
Run Code Online (Sandbox Code Playgroud)
当编译为32位应用程序时,这很好用,但是当编译为64位应用程序时,它始终返回true。
该文档没有提到对64位应用程序执行特殊操作。
如何繁殖:
program Project1;
uses
System.Types,
WinApi.windows,
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
function AttachConsole(dwProcessId: DWORD): Bool; stdcall; external KERNEL32 name 'AttachConsole';
begin
if AttachConsole(DWORD(-1)) then
begin
writeLN('Hello world');
Exit;
end;
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Run Code Online (Sandbox Code Playgroud)
在Win64下运行时,AttachConsole即使从资源管理器运行,也会重新运行true。
我已经编程了五年的第一个Delphi和现在的c#所以我想我知道重载方法是如何工作的,但显然不是.
首先是一些代码
public enum TestEnum { Option1, Option2, Option3 }
public class Setting
{
public Setting()
{
AddSettings();
}
protected void CreateSetting<TEnum>(string AName, TEnum AValue) where TEnum : struct, IComparable, IFormattable
{
//do stuff
}
protected void CreateSetting(string AName, string AValue)
{
//do stuff
}
protected void CreateSetting(string AName, int AValue)
{
CreateSetting(AName, AValue.ToString());
}
protected void AddSettings()
{
CreateSetting("Language", (byte)0); //#1
CreateSetting("BFL", "true"); //#2
CreateSetting<TestEnum>("TestEnum", TestEnum.Option1); //#3
CreateSetting("TestEnum", TestEnum.Option1); //#4
}
}
Run Code Online (Sandbox Code Playgroud)
我为每次调用CreateSettings添加了一个数字,以便更容易解释.
我的问题是:
调用#1调用CreateSettings的错误(通用)版本,因为我已经进行了一次转换,byte但是为什么呢?
呼叫#2 …
我在C#中使用Value Tuple玩耍。
首先是一些演示数据:
#region Data
public class Product
{
public string Name { get; set; }
public int CategoryID { get; set; }
}
public class Category
{
public string Name { get; set; }
public int ID { get; set; }
}
public class Data
{
public List<Category> Categories { get; } = new List<Category>()
{
new Category(){Name="Beverages", ID=001},
new Category(){ Name="Condiments", ID=002},
};
public List<Product> Products { get; } = new List<Product>()
{
new Product{Name="Cola", CategoryID=001},
new Product{Name="Tea", …Run Code Online (Sandbox Code Playgroud) I'm tyring calculate the maximum fontsize in order for at Text to fit into the ClientRect of a TCxLabel. But I cant get it to work probably. (See picture)

The fontsize is to big and the thxt is not drawn the corrent place.
Here how to reproduce:
Place a tcxLabel on an empty Form, and allign the label to client
Add a FormCreate and a FormResize event :
procedure TForm48.FormCreate(Sender: TObject);
begin
CalculateNewFontSize;
end;
procedure TForm48.FormResize(Sender: TObject);
begin
CalculateNewFontSize;
end; …Run Code Online (Sandbox Code Playgroud) 我需要检测 wifi 何时打开/关闭。为此,我正在使用 James Montemagno 的 Connectivity,但问题是如果手机可以访问移动网络并且我打开/关闭 wifi,我将不会收到 ConnectivityChanged 事件。
这是事件的映射:
CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
{
WiFiConnected = CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi);
};
Run Code Online (Sandbox Code Playgroud)
那么我可以检测到 Wifi 上的连接已更改吗?我想在 Xamarin Forms 代码中执行此操作,因此我不必为每个平台实施解决方案。
在我的Xamarin Forms项目中,我想定义每个平台的Form系列以及应用程序中的用法.
到目前为止,我已经对每个控件类型的FontFamily进行了硬编码
<Style x:Key="TahomaBase_Label" TargetType="Label" BaseResourceKey="SubtitleStyle">
<Setter Property="FontFamily" Value="Tahoma" />
...
</Style>
Run Code Online (Sandbox Code Playgroud)
是否可以在我的XAML代码中全局设置Fotnfamily而不是OnPlatform标签?
我想在以_(下划线)开头的字符串中查找单词.
这很容易
我写了这个小测试程序:
class Program
{
private static Regex WordExpression = new Regex(@"_\w+");
private static string TranslateWord(Match word) => word?.Value?.Replace("_", "");
private static string Translate(string word)
{
return WordExpression.Replace(word, TranslateWord);
}
static void Main(string[] args)
{
Console.WriteLine(Translate("Do you want to _Exit the _Program"));
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
而且效果非常好.当我的单词之间没有空格时,问题就开始了:
Console.WriteLine(Translate("_Exit_Program"));
Run Code Online (Sandbox Code Playgroud)
我的表达只找到一个匹配,_Exit_Program但我非常喜欢两个匹配.这可以用正则表达式完成,还是我需要在TranslateWord方法中执行拆分字符串?
我用C#编写了这个小程序
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(("7797302D875A8922EBFC7DECBD352FE88F35642F" == "?7797302D875A8922EBFC7DECBD352FE88F35642F").ToString());
var a = "7797302D875A8922EBFC7DECBD352FE88F35642F";
var b = "7797302D875A8922EBFC7DECBD352FE88F35642F";
MessageBox.Show((a == b).ToString());
}
Run Code Online (Sandbox Code Playgroud)
第一个messageBox显示"False",而Messagebox显示"True".
我的问题是:为什么我不能将这两个字符串与==运算符进行比较?
我在运行时从屏幕截图生成JPEG,并希望将其上传到FTP服务器.但是当我上传它时它不再是一个有效的jpeg.
在这个例子中,我只是从HD加载一个位图:
首先是一些代码:
procedure TForm13.Button1Click(Sender: TObject);
var
InBitmap: TBitmap;
JpegImage: TJpegImage;
MemoryStream: TMemoryStream;
IdFTP: TIdFTP;
begin
InBitmap := TBitmap.Create;
MemoryStream := TMemoryStream.Create;
JpegImage := TJpegImage.Create;
IdFTP := TIdFTP.Create(self);
try
InBitmap.LoadFromFile('C:\aa\test.bmp');
JpegImage.Assign(InBitmap);
JpegImage.CompressionQuality := 65;
JpegImage.SaveToStream(MemoryStream);
with IdFTP do
try
Host := <HOST>;
Username := <USER>;
Password := <PASS>;
Port := 21;
Passive := True;
Connect;
MemoryStream.Position := 0;
Put(MemoryStream, 'test.jpg');
finally
Disconnect;
end;
finally
IdFTP.Free;
JpegImage.Free;
InBitmap.Free;
MemoryStream.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
我试图在上传之前将JPEG保存到文件中,它是一个有效的JPG
我试图在上传之前将MemortStream保存到文件中,它是一个有效的JPG
但是当它上传到FTP服务器时,它只是一个空白的JPG文件,但仍然是"有效的".
它不是我的FTP服务器,而是托管我的域的人所拥有的服务器.这是刚刚上传的jpg的链接:http://fluffykids.dk/test.jpg,这是我保存到光盘的jpeg:http: …