C#10 并非所有代码都工作正常
我在autocad 2019 64位中的visual studio 2022中测试了C#10,但并非所有代码都工作正常。在我的代码的最后一部分中的数组索引示例请参见快照
如何使所有代码在C#10中正常工作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.DatabaseServices.Filters;
using System.Collections;
using static cadtest22_1.PersonClass;
namespace cadtest22_1
{
//1.test create struct Tipe T
public struct Foo
{
public T Var1;
public T Var2;
}
public struct Point
{
public double X { get; set; }
public double Y { get; set; }
public double Distance => Math.Sqrt(X * X + Y * Y);
public override string ToString() =>
$"({X}, {Y}) is {Distance} from the origin";
}
//2. test record
public class PersonClass
{
public string Name { get; }
public string Surname { get; set; }
public record PersonRecord
{
public string Name { get; set; }
public string Surname { get; set; }
public PersonRecord(string name, string surname)
{
Name = name;
Surname = surname;
}
public void Deconstruct(out string name, out string surname)
{
name = Name;
surname = Surname;
}
}
}
public class Class1
{
//3.test call
int M()
{
int y = 5;
int x = 7;
return Add(x, y);
static int Add(int left, int right) => left + right;
}
public void test1()
{
var doc = CadApp.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
//call struct foo
var d = new Foo { Var1 = 1, Var2 = 2 }; //ok
var dd = new Foo { Var1 = 1.1, Var2 = 20.1 }; //ok
CadApp.ShowAlertDialog($"struct foo T {d.Var1} - {d.Var2}"); //ok
// test switch multiple variable
string RockPaperScissors(string first, string second)
=> (first, second) switch
{
("rock", "paper") => "rock is covered by paper. Paper wins.",
("rock", "scissors") => "rock breaks scissors. Rock wins.",
("paper", "rock") => "paper covers rock. Paper wins.",
("paper", "scissors") => "paper is cut by scissors. Scissors wins.",
("scissors", "rock") => "scissors is broken by rock. Rock wins.",
("scissors", "paper") => "scissors cuts paper. Scissors wins.",
(_, _) => "tie"
};
CadApp.ShowAlertDialog($"{RockPaperScissors("rock","paper")}");
//add function without aggregate,action
int M()
{
int y = 5;
int x = 7;
return Add(x, y);
static int Add(int left, int right) => left + right;
} //ok
//test multiple switch
static int GetTax4(int muncipalityId) => muncipalityId switch
{
0 or 1 => 20,
> 1 and21,
> 5 and not 7 => 22,
7 => 23,
_ => 20
};
CadApp.ShowAlertDialog("value switch " + GetTax4(6).ToString());//ok
// test switch to autocad
static string ent(Entity myent) => myent.GetType().Name.ToString() switch
{
"Line" or "Polyline" or "Circle" => "curve",
_ => "Not curve"
};
var ge = ed.GetEntity("\nclick entiti");
if (ge.Status != PromptStatus.OK) return;
using(var tr = db.TransactionManager.StartTransaction())
{
var ent1 = tr.GetObject(ge.ObjectId, OpenMode.ForRead) as Entity;
string result = ent(ent1);
CadApp.ShowAlertDialog($"value switch {result}"); //ok
// test nullable
List numbers = null;
int? i = null;
numbers ??= new List();
numbers.Add(i ??= 17);
numbers.Add(i ??= 20);
// test nullable in autocad
var cur = ent1 as Curve;
Point3d? pt = cur?.StartPoint ?? (cur as Circle)?.Center;
CadApp.ShowAlertDialog($"value {cur.GetType().Name} {pt}");
}
// tes bool function is and or
bool IsLetter(char c) => c is >= 'a' and = 'A' andCadApp.ShowAlertDialog("testing dobule click"); //ok
var value = new[] { 10, 11, 12, 13 };
//this not working system index not import what must to do
int a = value[^1]; // 13
int b = value[^2]; // 12
int c = value;
}
}
}
**** Hidden Message ***** 要对 AutoCAD 进行编程,您将无法利用所有最新功能。使用名为“Class (.NET)”的模板开始您的类项目,而不是仅称为“Class”的模板,后者面向.NET Core。查看本文档,了解如何创建新项目并从中制作模板。对于 2019 年,您需要面向特定版本的 .NET Framework。pdf中还有一个图表,您的2019年项目需要针对哪个版本的.NET Framework。 与模板的关系
是什么 你有没有尝试过,并在net 5.0中工作
我已经在xml
预览中添加
并且netframework是4.8
@daboho.NET Framework 4.8 使用 C# 7.3,因此您的代码将仅限于其功能。
如前所述,AutoCAD需要.NET Framework,而不是.Net Core,也不是.Net 5.0
Records
和静态本地函数
,递归模式
和关系模式
以及合并分配
和索引运算符
和范围运算符
以及C# 7.3之后的其他好东西不可用,这是一个痛苦的问题。
问候,
补充:
这个
https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-450FD531-B6F6-4BAE-9A8C-8230AAC48CB4
记录了期望,
尽管可以使用更高版本的VS;即我正在使用VS2022与AutoCAD 2023(.net Framework 4.8) 正如kdub所重申的那样,您必须坚持AutoCAD版本的要求,2019年是.NET Framework 4.7。这也显示在提供的链接kdub中。就我提到的pdf而言,它告诉您如何正确创建新项目。它只描述了在最后制作模板。您现在遇到的问题是,您使用错误的模板创建了一个项目,并且无法以4.7框架为目标。若要修复问题,需要使用具有“类 (.NET)”字样的 C# 模板启动一个新项目。
自.NETFramework 2.0以来,所有这些功能都在F#中可用。 @gile感谢您的信息
@kdub您的视觉工作室2022和
Autocad 2023最新的语言版本是什么?
问候,
页:
[1]