Hi everyone!
I have some issues with AutoCAD 2025. When I reference to library files, it says 'Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found'. And I couldn't debug or create the application plugin. So any help in this issue will be greatly appreciated.
Thank you!
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
I guess you're trying to use Marshal.GetActiveObject. If so, you should see this topic.
Thank you for sharing the link. But I need a proper solution where I know the exact solution like how to resolve the issue. A step by step guide will be very appreciated.
You can create the following class to your project (referencing System.Runtime.InteropServices).
public class MarshalExtension
{
[DllImport("ole32")]
private static extern int CLSIDFromProgIDEx(
[MarshalAs(UnmanagedType.LPWStr)] string lpszProgID,
out Guid lpclsid);
[DllImport("oleaut32")]
private static extern int GetActiveObject(
[MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
IntPtr pvReserved,
[MarshalAs(UnmanagedType.IUnknown)] out object ppunk);
public static object? GetActiveObject(string progId,
bool throwOnError = false)
{
ArgumentNullException.ThrowIfNull(progId);
var hr = CLSIDFromProgIDEx(progId, out var clsid);
if (hr < 0)
{
if (throwOnError)
Marshal.ThrowExceptionForHR(hr);
return null;
}
hr = GetActiveObject(clsid, IntPtr.Zero, out var obj);
if (hr < 0)
{
if (throwOnError)
Marshal.ThrowExceptionForHR(hr);
return null;
}
return obj;
}
}
Then, instead of: Marshal.GetActiveObject(progId); simply call:
MarshalExtension.GetActiveObject(progId);
This is the code. Error occurrs here "PromptEntityResult result = ed.GetEntity(prompt);" and at many places.
using System;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
[assembly: CommandClass(typeof(ExportSurfacePoints.Plugin))]
namespace ExportSurfacePoints
{
public class Plugin
{
[CommandMethod("ExportSurfacePoints")]
public void ExportSurfacePoints()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
CivilDocument civilDoc = CivilApplication.ActiveDocument;
// Prompt user to select a surface
PromptEntityOptions prompt = new PromptEntityOptions("\nSelect a surface:");
prompt.SetRejectMessage("\nSelected object is not a surface.");
prompt.AddAllowedClass(typeof(TinSurface), true);
PromptEntityResult result = ed.GetEntity(prompt);
if (result.Status != PromptStatus.OK) return;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
TinSurface surface = tr.GetObject(result.ObjectId, OpenMode.ForRead) as TinSurface;
if (surface == null)
{
ed.WriteMessage("\nError: Selected object is not a valid TIN Surface.");
return;
}
// Ask user to provide file name and location
PromptSaveFileOptions saveFileOptions = new PromptSaveFileOptions("\nEnter file path to save surface points:");
saveFileOptions.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
PromptFileNameResult fileNameResult = ed.GetFileNameForSave(saveFileOptions);
if (fileNameResult.Status != PromptStatus.OK) return;
string filePath = fileNameResult.StringResult;
// Export surface points to a file
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("X,Y,Z");
foreach (TinSurfaceVertex vertex in surface.Vertices)
{
writer.WriteLine($"{vertex.Location.X},{vertex.Location.Y},{vertex.Location.Z}");
}
}
ed.WriteMessage($"\nSurface points exported to: {filePath}");
tr.Commit();
}
}
}
}
The errors are "Reference to type 'ICloneable' claims it is defined in 'System.Runtime', but it could not be found" and "Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found".
When I use the same code with 2024 libs, it works. But it gives me these errors when use with 2025.
With AutoCAD 2025, you have to target .NET (Core) 8.0. See this topic.
If you start a project from scratch, you have to start from a class library template targetting .NET or .NET Standard as shown in this topic.
You can also start from some AutoCAD specific .NET Template as this one (attentively read the 'README' to correctly edit the template files).
Another way could be migrating a working project targetting .NET Framework to .NET 8 as desrcribed in this thread.
To address the issue of 'Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found' in AutoCAD 2025, the error typically occurs due to mismatches in the .NET framework libraries that are referenced in your project. Here are some steps you can take to resolve this:
1. Check .NET Framework Version:
Ensure that your AutoCAD 2025 plugin or application is targeting the correct .NET framework version. AutoCAD 2025 might require a newer version of .NET than what you're currently using.
@nomanmurtaza95 a écrit :
Make sure it's set to the correct version compatible with AutoCAD 2025 (typically .NET Framework 4.7 or above).
From the developer's documentation:
If you are targeting AutoCAD 2025 or AutoCAD 2025-based programs, you should use:
Can't find what you're looking for? Ask the community or share your knowledge.